[英]window.onload != <body onload=“”>
This is rather interesting, I think. 我认为这很有趣。 Consider following code, both the window.onload and body onload="" call the same function. 考虑以下代码,window.onload和body onload =“”都调用相同的函数。 However, the results are different. 但是,结果不同。 It appears to me that window.onload has a problem with collections. 在我看来,window.onload存在集合问题。 Here's the code: 这是代码:
<html>
<script type="text/javascript">
window.onload = getSpanElements();
function getSpanElements(){
var collectionBoolean = document.getElementsByTagName("span")?true:false;
alert(
"collection exists? " + collectionBoolean + "; number of collection members: " + document.getElementsByTagName("span").length
);
}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body onload="getSpanElements()">
<span> test </span>
</body>
As you can see, both report that the collection exists, however window.onload reports that it has no members. 如您所见,两者都报告集合存在,但window.onload报告它没有成员。 Any ideas? 有任何想法吗?
You're setting the function wrong: 您正在设置错误的功能:
window.onload = getSpanElements();
should be 应该
window.onload = getSpanElements;
You're setting the onload handler to the return value of getSpanElements() at the moment. 您现在将onload处理程序设置为getSpanElements()的返回值。
window.onload = getSpanElements();
should be 应该
window.onload = getSpanElements;
The code you have calls the getSpanElements
function and assigns its return value as the onload event handler. 您调用的代码调用 getSpanElements
函数并将其返回值指定为onload事件处理程序。
You're wrongly doing this: 你做错了:
window.onload = getSpanElements();
which sets the window.onload
to the result of the call to the function getSpanElements
(undefined). 它将window.onload
设置为调用函数getSpanElements
(undefined)的结果。
You should do this instead: 你应该这样做:
window.onload = getSpanElements;
You might want to move your window.onload assignment below the getSpanElements declaration: 您可能希望将window.onload分配移动到getSpanElements声明下方:
<html>
<script type="text/javascript">
function getSpanElements(){
var collectionBoolean = document.getElementsByTagName("span")?true:false;
alert(
"collection exists? " + collectionBoolean + "; number of collection members: " + document.getElementsByTagName("span").length
);
}
window.onload = getSpanElements;
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body onload="getSpanElements()">
<span> test </span>
</body>
At the point in your code where you're assigning the window.onload event handler, getSpanElements() has not yet been defined. 在代码中您指定window.onload事件处理程序的位置,尚未定义getSpanElements()。 Also, the line should be 此外,该行应该是
window.onload=getSpanElements;
not 不
window.onload=getSpanElements();
The function name without parentheses is a reference to the function. 没有括号的函数名称是对函数的引用。 With parentheses, it executes the function and the return value is assigned to window.onload. 使用括号,它执行函数,并将返回值分配给window.onload。
You have to assign a reference to the function getSpanElements
to window.onload
- currently, the function doesn't get executed onload
, but immediately after parsing. 您必须将函数getSpanElements
的引用分配给window.onload
- 当前,该函数不会在onload
执行,而是在解析后立即执行。
What you actually assign is the undefined return value. 您实际分配的是未定义的返回值。
In short: drop the ()
. 简而言之:放弃()
。
我认为window
对象是在解析任何实际元素之前创建的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.