jquery - IE9 js加载顺序和JQuery

标签 jquery internet-explorer internet-explorer-9

这似乎是一个很容易解决的问题,但它却让我很烦恼。

以下简单示例在 IE9 中产生错误。我使用 IE9 的开发人员页面来监视网络加载,看起来 IE 正在加载/评估包含的 javascript 之前加载并执行正文的脚本。

我已经尝试了通过搜索答案找到的所有各种技巧,包括添加 <meta charset="UTF-8" /><meta http-equiv="x-ua-compatible" content="IE8"/>

<html>
<head>
  <title>Demo of IE suckiness</title>
  <script type='text/javascript' 
      src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
  </head>
  <body>
This works in every browser but not IE9. IE gives the error:
<pre>
Line: 9
Error: The value of the property '$' is null or undefined, not a Function object
</pre>

<script type="text/javascript" defer="defer">
$(document).ready(function(){
  alert("Must not be IE");
});
</script>
</body>
</html>

我有一个解决方法。将准备好的脚本移至<body onload="ready()"> ,但我不敢相信这对 IE9 来说是坏的,我一定做错了什么,只是看不到它。

最佳答案

据我所知,IE9 仅在整个文档(包括所有图像和相关内容)加载后才会触发 $.load() 。可怕但真实!我的解决方法:

            var docReady = {
                actions:new Array(),
                flag:0,
                time:0,
                period:10,
                trigger:function() {this.flag=1},
                checkHandle:0,
                check:function() {
                    this.time+=this.period;
                    if(this.flag) {
                        clearInterval(this.checkHandle);
                        for(i in this.actions) {this.actions[i]();}
                    }
                },
                watch:function() {
                    this.checkHandle = setInterval(function() {docReady.check();},this.period);
                },
                action:function(f) {
                    this.actions.push(f);
                }

            };

            docReady.watch();

确实是一个肮脏的东西,但它正在工作;现在,只要您认为合适,您就可以调用 docReady.trigger(),并且至少应该在页面末尾执行此操作。另外,您可以将其称为 body onload。确保您使用 docReady.action() 添加的函数只会运行一次:当第一个 trigger() 发生时。

关于jquery - IE9 js加载顺序和JQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7988405/

相关文章:

javascript - 如何在 onclick 事件中对 function() 以外的按钮使用以下代码?

jquery轮播问题

css - 背景图片不显示 IE8

api - YouTube iframe API - onReady 和 onStateChanged 事件在 IE9 中未触发

css - 在 IE 中使图像在两条线之间居中

javascript - 从 Javascript 中排除链接

javascript - 如何使用 Javascript 隐藏页面加载时的特定句子

angular - 使用 Angular 项目时,只要打开开发人员工具,Internet Explorer 11 就会崩溃

jquery - 使用 Dynatree 包装文件夹

jquery - IE6 下的 AJAX 极其缓慢