使用window.onload超过1次 [英] Using window.onload more than 1 times
本文介绍了使用window.onload超过1次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我写这个
$(document).ready( function () {
alert("bla1");
});
$(document).ready( function () {
alert("bla2");
});
所有好,两个警报发生,但如果代码是
All good, two alert occurs, but if code is
window.onload = function () {
alert("bla1");
}
window.onload = function () {
alert("bla2");
}
第二次警报未发生,请告诉某人,为何2或更多 window.onload 是错误的?
second alert not happened, tell someone please, why 2 or more window.onload is wrong?
推荐答案
你为它指定了一些东西。然后你为它分配一些其他内容并覆盖之前的内容。
You assign something to it. You then assign something else to it and overwrite what is there before.
foo = 1;
foo = 2;
// foo is now "2" it is not "1" and "2"
如果要非破坏性地分配事件处理程序,请使用 的addEventListener
。您需要使用兼容性方法来支持Old-IE。这些方法通过 on
方法。
If you want to assign event handlers non-destructively, use addEventListener
. You will need to use compatibility methods to support Old-IE. Such methods are built into jQuery via the on
method.
这篇关于使用window.onload超过1次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文