我有两个文件要处理。第一个文件是主页,它使用.load()显示第二个文件。Safari在第一个页面上运行jquery很好,但是它似乎没有在通过.load()检索到的文件中运行jquery。我尝试将alert()作为第一行放入
$(document).ready(function(){});
而且它根本不能在Safari中运行。
在Chrome中,所有的jquery都按预期运行。有可能是什么原因造成的吗?
编辑:这是我遇到的问题的一个小例子:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#loadStuffHere').load('example1b.html');
});
</script>
</head>
<body>
<div id="loadStuffHere"></div>
</body>
</html>
这是第二页(example1b.html):
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#test').click(function() {
alert("This code executes in Chrome but not Safari.");
});
});
</script>
</head>
<body>
<p id="test">This is what is being loaded</p>
</body>
</html>
发布于 2011-09-08 02:55:40
我认为你的主要问题源于加载整个超文本标记语言页面,<html>
,<head>
和所有的东西到你的div。如果从字面上看是这样的,那么在加载之后,您将得到如下所示的页面结构:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#loadStuffHere').load('example1b.html');
});
</script>
</head>
<body>
<div id="loadStuffHere"><html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#test').click(function() {
alert("This code executes in Chrome but not Safari.");
});
});
</script>
</head>
<body>
<p id="test">This is what is being loaded</p>
</body>
</html></div>
</body>
</html>
...which显然是一个非常无效的超文本标记语言文档,它包含多个<html>
、<body>
和<head>
元素。
load()
通常用于将一段超文本标记语言加载到页面的元素中,因此最终得到的页面总体上仍然是有效的。
在将HTML加载到页面中时,浏览器通常会有一些保护措施,防止出现无效文档,同样,浏览器会解析页面中的错误HTML,以便最大限度地利用页面。
在这种情况下,我猜测Safari可能会以不同于Chrome的方式来处理您试图将布局压缩到页面中的尝试--例如,它可能会忽略<head>
部分中的<script>
,因为它在第二个<head>
中,并且它已经看到了其中之一。
请记住,<script>
元素不必在HTML文档的<head>
中;它们也可以添加到页面的<body>
中的任何位置。
最后,您也不需要在加载的片段中加载jQuery --您将代码插入到已经加载了jQuery的页面中,因此您加载的脚本无论如何都可以访问它。
https://stackoverflow.com/questions/7311106
复制相似问题