Javascript 为什么可以';我是否在document.onload中编写代码而不是放置函数?
我能够做到这一点:Javascript 为什么可以';我是否在document.onload中编写代码而不是放置函数?,javascript,Javascript,我能够做到这一点: document.onload = init(); 但不是这个: document.onload = function() { alert('Test'); } 为什么 编辑: 这是init() 我需要在函数周围添加一些括号,然后在末尾添加另一对括号来调用它: document.onload = (function() { init(); })() 试试这个: document.onload = init(){ alert('Test'); } 重载
document.onload = init();
但不是这个:
document.onload = function() {
alert('Test');
}
为什么
编辑:
这是init()
我需要在函数周围添加一些括号,然后在末尾添加另一对括号来调用它:
document.onload = (function() {
init();
})()
试试这个:
document.onload = init(){
alert('Test');
}
重载
init
函数,document.onload
最初调用init
函数。document.onload=init()
只不过是调用init
functin并将返回值赋给document.onload
<代码>document.onload=init代码>我认为第二个是可行的,而不是第一个。我在init
中放置了一个alert()
。当我执行document.onload=init()时
弹出警报,但当我执行document.onload=init时代码>它没有。为什么?在这种情况下,您甚至不需要document.onload=
。只要用生命
document.onload = init(){
alert('Test');
}