Safari jquery兼容性 [英] Safari jquery compatibility

查看:117
本文介绍了Safari jquery兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件,我处理。第一个文件是主页面,它使用.load()来显示第二个文件。 Safari在第一页上运行jquery很好,但似乎没有运行通过.load()检索的文件中的jquery。我尝试在

I have 2 files I'm dealing with. The first file is the main page and it uses .load() to display the second file. Safari runs the jquery on the first page just fine but it doesn't seem to be running the jquery in the file that is retrieved through .load(). I tried putting an alert() as the first line in

$(document).ready(function(){});

它只是不在Safari中运行。

and it simply isn't run in Safari.

在Chrome中,所有jQuery都按预期运行。任何线索可能是什么导致这?

In Chrome, all the jquery runs as expected. Any clue what might be causing this?

编辑:这里是我遇到的问题的一个小例子:

edit: here is a small example of the problem I am having:

<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):

and here is the second page (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>


推荐答案

我认为你的主要问题来自于加载整个HTML页面,< html> < head> 如果这样工作的话,你会得到一个页面结构,像这样加载后:

I think your main problem stems from loading an entire HTML page, <html>, <head> and all into your div. If that worked literally, you would end up with a page structure like this after the load:

<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>

...这显然是一个很无效的HTML文档,包含多个< html> < body> < head> 元素。

...which is clearly a pretty invalid HTML document, containing as it does multiple <html>, <body> and <head> elements.

load()通常用于将HTML的片段

load() is typically used to load a fragment of HTML into an element of your page, so you end up with an altered page that's still valid HTML overall.

浏览器通常有一些保护措施,防止在加载HTML时将无效文档结尾

Browsers generally have some protection against ending up with invalid documents when loading HTML into a page, in the same way they parse bad HTML in pages to make the best they can of them.

在这种情况下,我猜想Safari可能会处理您的尝试例如,它可以忽略< code>中的< script> 头> 部分,因为它在第二个< head> ,并且已经看到其中的一个。

In this case, I'm guessing that Safari may handle your attempt to crowbar the layout into the page differently from Chrome -- it's possible, for example, that it simply ignores the <script> in the <head> section because it's in a second <head> and it's already seen one of those.

请记住,< script> 元素不必在< head> HTML文档;它们可以添加到页面的< body> 中的任何位置。

Bear in mind that <script> elements don't have to be in the <head> of HTML documents; they can be added anywhere in the <body> of a page, too.

最后,不需要在加载的片段中加载jQuery - 您将代码插入到已经加载了jQuery的页面中,因此加载的脚本将可以访问它。

Finally, you also don't need to load jQuery in the loaded fragment -- you're inserting your code into a page that already had jQuery loaded, so your loaded script will have access to it anyway.

这篇关于Safari jquery兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆