jQuery $(document).ready() 在 IE 8 中如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/747854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:42:51  来源:igfitidea点击:

How does $(document).ready() work in IE 8?

jqueryinternet-explorer

提问by MojoFilter

I've recently installed IE 8 and can't seem to get the jquery $(document).ready event to fire. Are there any special considerations that I'm missing? Litterally, this is all I have in my html and it works as expected in Chrome and Firefox:

我最近安装了 IE 8,但似乎无法触发 jquery $(document).ready 事件。是否有任何我遗漏的特殊注意事项?从字面上看,这就是我的 html 中的全部内容,它在 Chrome 和 Firefox 中按预期工作:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page full of awesomeness</title>
    <script type="text/javascript" src="~/Scripts/jquery-1.3.2.js" />
    <script type="text/javascript">        
        $(document).ready(function() {
            alert("Hello?");           
        });
    </script>
</head>

<body>

</body>

In Internet Explorer, the page just loads without incident. There's no alert box and I can't see any javascript errors reported. Is this something normal that I just don't know about?

在 Internet Explorer 中,页面会正常加载。没有警告框,我看不到任何 javascript 错误报告。这是我不知道的正常现象吗?

回答by ólafur Waage

Try turning this.

试试转这个。

<script type="text/javascript" src="~/Scripts/jquery-1.3.2.js" />

Into this

成这个

<script type="text/javascript" src="~/Scripts/jquery-1.3.2.js"></script>

回答by Travis

With current XHTML strict standards:

使用当前的 XHTML 严格标准:

Even when srcis specified, the script tag is not an empty tag, and cannot be written <script src=".... />. If you include the srcyou should not include any script between the opening and closing tags as browser handling of any script between the tags is not reliable.

即使src指定了,脚本标签也不是空标签,不能写入<script src=".... />。如果包含 ,则src不应在开始和结束标记之间包含任何脚本,因为浏览器对标记之间的任何脚本的处理都不可靠。

Basically, do not self close the tag. Use </script>.

基本上,不要自行关闭标签。使用</script>.

回答by rfunduk

In addition to what others have said, you're also missing the </html>at the end of the document. Maybe just a copy/paste error :)

除了其他人所说的之外,您还缺少</html>文档末尾的 。也许只是一个复制/粘贴错误:)

回答by Sven P

Also check the compatibility of jQuery. Currently jQuery 2.x only supports IE9 or later. Not IE8

还要检查 jQuery 的兼容性。目前 jQuery 2.x 仅支持 IE9 或更高版本。不是 IE8

回答by Murad

$(document).ready()

Not work in IE8 I find a code sample from this link https://plainjs.com/javascript/events/running-code-when-the-document-is-ready-15/It is working with Jquery 1.10.2

在 IE8 中不起作用我从这个链接https://plainjs.com/javascript/events/running-code-when-the-document-is-ready-15/找到了一个代码示例 它正在使用 Jquery 1.10.2

 function run() {
    // do something
    alert('working');
}

// in case the document is already rendered
if (document.readyState!='loading') run();
// modern browsers
else if (document.addEventListener) document.addEventListener('DOMContentLoaded', run);
// IE <= 8
else document.attachEvent('onreadystatechange', function(){
    if (document.readyState=='complete') run();
});

回答by Alekc

My guess would be this (sorry i don't have ie8 on this machine to test)

我的猜测是这个(对不起,我在这台机器上没有 ie8 来测试)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page full of awesomeness</title>
    <script type="text/javascript" src="~/Scripts/jquery-1.3.2.js"></script>
    <script type="text/javascript">        
        $(document).ready(function() {
            alert("Hello?");           
        });
    </script>
</head>

<body>

</body>

Also i'd suggest to use /Scripts/jquery-1.3.2.jsif you are referring to your site's root

此外,/Scripts/jquery-1.3.2.js如果您指的是您网站的根目录,我建议您使用