javascript “DomContentLoaded”在我的浏览器中不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10442129/
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-10-26 09:54:26  来源:igfitidea点击:

"DomContentLoaded" isn't working in my browser?

javascriptgoogle-chromeaddeventlistener

提问by Hyman Zhang

I wrote my html page like this:

我这样写我的html页面:

<div>
    <img src="Natural1.jpg" id="img1" >
 <audio src="sound.mp3" id="audio1" ></audio>
</div>

And my javascript file is this:

我的 javascript 文件是这样的:

function init(){
    audio1 = document.getElementById("audio1");
    var img1 = document.getElementById("img1");
    img1.addEventListener("click", imgClick, false);
}
function imgClick(){
    if(audio1.paused){
        audio1.play();
    }
    else{
        audio1.pause();
    }
}
document.addEventListener('DomContentLoaded', init, false);

I run that in chrome12, the script first execute the document.addEventListenermethod, but it did not go into the init method, why? I tried attachEventmethod instead addEventListenerin IE8, but it still doesn't. what's wrong with my code?

我在chrome12中运行那个,脚本先执行了document.addEventListener方法,但是没有进入init方法,为什么呢?我在 IE8 中尝试了attachEvent方法addEventListener,但仍然没有。我的代码有什么问题?

回答by Sampson

Might be the casing of th event name. Try the following instead:

可能是事件名称的大小写。请尝试以下操作:

document.addEventListener('DOMContentLoaded', init, false);

Pretty sure this is the case. I tested the following:

很确定就是这种情况。我测试了以下内容:

document.addEventListener("DomContentLoaded", function(){ alert('Dom') }, false);
document.addEventListener("DOMContentLoaded", function(){ alert('DOM') }, false);

On jsbin, and the latter event was raised alone. Note, IE8 will not raise this alert since this event doesn't take place there. Instead, you will find success from IE9 and IE10.

在 jsbin 上,后一个事件是单独引发的。请注意,IE8 不会引发此警报,因为此事件不会发生在那里。相反,您会发现IE9 和 IE10 的成功。

Example: http://jsbin.com/ocidok/edit#javascript,html

示例:http: //jsbin.com/ocidok/edit#javascript,html

Browser support for DOMContentLoadedis cataloged on the Mozilla Developer Network. As of today, this event is only available in the following browsers:

Mozilla 开发人员网络DOMContentLoaded上对 的浏览器支持进行了编目。截至今天,此活动仅在以下浏览器中可用:

enter image description here

在此处输入图片说明

回答by realkstrawn93

JavaScript event names are case-sensitivestrings. Therefore, capital and lowercase letters are NOTinterchangeable in event names. So, changing the name from 'DomContentLoaded'to 'DOMContentLoaded'will fix the issue -- in Chrome at least.

JavaScript 事件名称是区分大小写的字符串。因此,大写和小写字母在事件名称中不可互换。因此,将名称从 更改'DomContentLoaded''DOMContentLoaded'将解决该问题——至少在 Chrome 中是这样。

As for IE 8: There's no way. The first version of IE to ever implement the DOMContentLoadedamong other HTML5 events was version 9, so all I can say is: Update your browser! I also noticed that you're using the 16-version-out-of-dateChrome 12, am I right? Seriously, both browsersneed to be updated. Outdated browsers pose serious security risks. Good thing I as a Chromebook user am able to keep the most critical component to computer security -- the browser -- up-to-date VERY easily due to its seamless nature...

至于 IE 8:没办法。第一个实现DOMContentLoadedHTML5 事件的 IE 版本是版本 9,所以我只能说:更新您的浏览器!我还注意到您使用的是16 版本过时的Chrome 12,对吗?说真的,两个浏览器都需要更新。过时的浏览器会带来严重的安全风险。好在我作为 Chromebook 用户能够保持计算机安全最关键的组件 - 浏览器 - 由于其无缝性质而非常容易地保持最新......