在单独的php头文件中包含jquery会失败
Including jquery in a separate php header file somehow fails
我正在建立一个网站,并且一直在使用一些似乎不起作用的jQuery脚本。但是过了一段时间,我发现我必须链接到我的内容文件中的jQuery库。
我的层次结构如下:
- header.php (common header html)
- link/index.php(其中"link"是域名上的任何子文件夹,即:contact/index.php, home/index.php等)
- footer。php
所有这些内容文件都使用了"require_once("header.php")",对于页脚,模板技术也是如此。
即使在浏览器中查看源代码成功地链接到包含的脚本和CSS文件,它似乎不加载jQuery(只)。但是当我将它包含在特定的内容文件中时,我编写的脚本就可以工作了。
下面是一些相关的代码:
在header。php:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="NO">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script type="text/javascript" src="../js/jquery-latest.pack.js"></script>
<script type="text/javascript" src="../js/shl_menu.js"></script>
<script type="text/javascript" src="../js/prototype.js"></script>
<script type="text/javascript" src="../js/scriptaculous.js?load=effects,builder"></script>
<script type="text/javascript" src="../js/lightbox.js"></script>
In gallery/index.php (content file)
<?php require_once("../header.php"); ?>
<!-- Sub Menu List -->
<nav id="shl-submenu" class="left shadow-box rounded-corners">
<script type="text/javascript">
$(document).ready(function(e)
{
$("#shl-submenu ul li a").click(function(e) {
alert("Click");
});
});
...
现在,如果我在脚本上面包含<script type="text/javascript" src="../js/jquery-latest.pack.js"></script>
,它可以工作,但除非。
任何帮助都将非常感激!谢谢。
正如您所猜测的,jQuery和Prototype之间存在冲突。两者都将$定义为函数。你可以使用变量jQuery
而不是$
来避免这种情况,并在jQuery之后声明prototype。这篇文章解释了一个处理这个问题的好方法:
如何避免JQuery与Prototype之间的冲突
相关文章:
- 在JavaScript中包含jQuery
- Python 在包含 JQuery 时抛出 KeyError
- 如何在javascript模块中包含jquery而不与全局名称空间冲突
- 如何在One'中包含jQuery.js;s Own.js在$(document).ready之前
- 包含jquery的Javascript在Internet Explorer中不起作用
- Chrome扩展-当我包含jQuery库时,JS为什么会抱怨
- 在我的页面上包含jquery库有什么缺点吗
- 在js文件中包含jquery
- 包含jquery对象的javascript对象
- 在 WordPress-plugin 中包含 jQuery
- 包含 jQuery 会导致标准 JavaScript 停止运行
- 包含 jquery.无冲突
- 我们可以在 Java 脚本验证函数中包含 jQuery 吗?
- 如何替换包含 jquery 捕获的 html 的变量中出现的字符串
- 如何在我的 Rails 应用程序的一个页面上仅包含 JQuery 库
- 如何在外部.js文件中包含 jQuery
- 如何包含 jquery.validate.js 文件(在本地目录中)并在表单验证中使用它
- 如何使 IDE 识别出我在 HTML 文件中包含 jquery
- 在页面中包含jquery.js和javascript.js
- 当通过导入包含 jQuery 时,窗口加载事件不会触发