简体   繁体   English

Javascript window.onload在IE中不起作用

[英]Javascript window.onload not working in IE

I wrote code that should clear form when user open page. 我写了应该在用户打开页面时清除表格的代码。 It's working in FF but not in IE, any idea why? 它在FF中工作,但在IE中不工作,为什么?

window.onload = clearForm()

  function clearForm() 
 {  

("load event detected!");  

 };

This line: 这行:

window.onload = clearForm()

calls clearForm and then assigns its return value to window.onload , exactly like x = foo(); 调用 clearForm ,然后将其返回值分配给window.onload ,就像x = foo(); calls foo and assigns the result to x . 调用foo并将结果分配给x Remove the parens: 删除括号:

window.onload = clearForm

Separately, I would strongly recommend not relying on the horror that is automatic semicolon insertion . 另外,我强烈建议您不要依赖自动分号插入的恐怖。 Always supply all required semicolons: 始终提供所有必需的分号:

window.onload = clearForm;

(Amusingly, you don't need the one at the end of your function clearForm() { ... } , because that's a function declaration, not a statement. It's harmless, though.) (有趣的是,您不需要function clearForm() { ... }的末尾使用它,因为那是一个函数声明,而不是声明。但这是无害的。)

Change window.onload = clearForm() to window.onload = clearForm; window.onload = clearForm()更改为window.onload = clearForm; otherwise because of () you are assigning result of your function to window.onload 否则由于()您正在将函数的结果分配给window.onload

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM