我在 Twitter 上看到了这个问题,也无法解释。以下两种方式定义 onload
函数都可以:
1)JSFiddle
<html>
<head>
<script>
onload = function(){
alert('this works');
};
</script>
</head>
<body>
</body>
</html>
2) JSFiddle
<html>
<head>
<script>
window.onload = function(){
alert('this works');
};
</script>
</head>
<body>
</body>
</html>
但是,如果定义如下,即使被分配到
window.onload
上,它也不起作用。
3)JSFiddle
<html>
<head>
<script>
function onload(){
alert('this doesnt work');
};
alert(window.onload); // this shows the definition of above function
</script>
</head>
<body>
</body>
</html>
这里发生了什么?