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

标签 javascript jquery joomla

我正在创建一个新的 Joomla 模板,但在链接到外部 .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 中定义了文件和文件夹,尽管我使用完全相同的方法来链接 CSS,但除 js 之外的所有内容都工作正常。帮帮我吧,我很困惑。

最佳答案

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

还可以像这样包装您的代码:

(function($) {

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

        alert(1);
    });


})(jQuery);

这为您的 Jquery 代码保留 $。

关于javascript - 如何让外部 .JS 文件在 joomla 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31293032/

相关文章:

javascript - 加载初始网站后在后台加载数据

javascript - 使用 Enter 触发 .click 函数

joomla - 如何重命名 joomla 组件?

没有菜单项的 Joomla 博客文章 - 使用了错误的模块

joomla - 如何编辑我的 Joomla 登录页面?

javascript - 在响应容器内使图像高度响应

javascript - 如何正确返回对象方法

Javascript 函数在取消选中复选框时不起作用

javascript - jquery datepicker 设置 mindate

jquery - 如何防止jquery下降太多?