如何让外部 .JS 文件在 joomla 中工作

How to get external .JS files to work in joomla

我正在创建一个新的 Joomla 模板,运行 遇到 link 外部 .js 文件的问题。

    <head>
<jdoc:include type="head" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/system.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/general.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/main.css">
<link rel="stylesheet" type="text/css" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/FLpresentation.css">
<script src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/js/jquery-2.1.4.js"></script>
<script src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/js/FLpresentation.js"></script>
<script src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/js/FLajax.js"></script>

上面的代码包含在 head 标签中,当我将模板安装到 joomla 时,CSS 工作正常。一切看起来都应该如何,但我的所有 Jquery 和 Javascript 代码都不起作用。唯一的代码是在 index.php 文件的标签内。

谁能告诉我我做错了什么?我已经在 templateDetails.xml 中定义了文件和文件夹,尽管我对 link 和 CSS 使用了完全相同的方法,但 js 以外的所有内容都工作正常。帮帮我,我很困惑。

100% JS 冲突。只需检查控制台!您包括 Jquery 库,但 Joomla 已经带有 Jquery。检查控制台是否有错误,检查输出 HTML 中的 Jquery 是否超过 1 次并迁移代码。

也像这样包装你的代码:

(function($) {

    $(document).ready(function(){
        // START HERE FOR ON_DOCUMENT_READY

        alert(1);
    });


})(jQuery);

这会为您的 Jquery 代码保留 $。

使用 JHtml::script 方法,如下例所示:

$url = Juri::base();// pick the url for my joomla website
JHtml::script($url . 'components/com_revista/prefixfree.min.js');//here i'm using one file in my server, but you could put the url for your script like 
"JHtml::script('http://urlforscriptsource/script.js');"

下面的代码在您的 joomla 扩展上添加 JQuery 框架:

JHtml::_('jquery.framework');
JHtml::_('jquery.ui');
JHtml::_('jquery.ui', array('sortable'))

;

Ps:最好在你的 JQuery 代码中使用 JQuery() 而不是 $(),这样可以避免冲突!