简体   繁体   English

奇怪的document.body上的onload事件

[英]onload event on document.body behaving strange

I am using firefox 4.0 and when i use load event on the body element it behaves strange. 我正在使用firefox 4.0,当我在body元素上使用load事件时,它的行为很奇怪。 i have tried some things and some work and some dont. 我尝试了一些东西,一些工作,有些没有。 the first is like this 首先是这样的

<html>
<head>
<script type="text/javascript">
var load = function(){
alert("body loaded");      //alert not displayed
};
document.body.addEventListener("load",load,false);
</script>
</head>
<body >
</body>
</html>

now this doesnt work now i tried this 现在这不起作用现在我尝试了这个

     html>
    <head>
    <script type="text/javascript">
    var load = function(){
    alert("body loaded");      //alert displayed
    };
    </script>
    </head>
    <body onload="load()">
    </body>
    </html>

this works now i tried this 这有效,现在我尝试了

<html>
<head>
<script type="text/javascript">
var load = function(){
alert("body loaded");       //alert not displayed
};
document.body.onload=load;
</script>
</head>
<body >
</body>
</html>

this doesnt work. 这行不通。 Whats wrong? 怎么了?

as response to Doug D i tried this 作为对道格D的回应,我尝试了这个

<html>
<head>
</head>
<body>
<script type="text/javascript">
var load = function(){
alert("body loaded");       //alert not displayed
};
document.body.onload=load;
</script>
</body>
</html>

this doesnt work either neither does this work 这不起作用也不起作用

<html>
<head>
</head>
<body>
</body>
<script type="text/javascript">
var load = function(){
alert("body loaded");       //alert not displayed
};
document.body.onload=load;
</script>
</html>

in the last attempt the body has finished loading and in the second last attempt body is being loaded . 在最后一次尝试中, body has finished loading而在第二次尝试中, body is being loaded but this does not work still 但这仍然无法正常工作

The body can't be accessed in the head until it has been loaded. 在加载之前,无法在头部访问body Your second technique of using the onload attribute is correct. 使用onload属性的第二种技术是正确的。 Alternately, I would recommend using jQuery with 或者,我建议将jQuery与

$(document).ready(function()
{
    alert("body loaded");
});

It is cross-browser compatible. 它与跨浏览器兼容。 Without jQuery you would need to deal with DOM event differences between IE and others. 没有jQuery,您将需要处理IE与其他人之间的DOM事件差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iPhone Safari游戏中的奇怪点击事件泡泡,它在冒泡到document.body之前停止 - Strange Click Event Bubble on iPhone safari that it stop before bubbling to document.body event.preventDefault不适用于Chrome中的“ document” /“ document.body”元素 - event.preventDefault is not working for “document” / “document.body” elements in Chrome 如何将事件冒泡到文档而不是文档。 - How can an event bubble to document but not to document.body? document.Body ComponentQuery - document.Body ComponentQuery document.body为null - document.body is null Opera 中的 Document.body - Document.body in Opera Javascript:除非元素附加到document.body,否则不会触发事件 - Javascript: Event does not fire unless element appended to document.body 在document.body上的Click事件无法在IE8上正常运行 - Click event on document.body not working as it should on IE8 将大量元素事件侦听器委托给 document.body 是不是很疯狂? - Is it crazy to delegate tons of element event listeners to document.body? Javascript onload无法正常工作(“未捕获的TypeError:无法将属性&#39;document.body&#39;设置为null”) - Javascript onload isn't working (“Uncaught TypeError: Cannot set property 'document.body' of null ”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM