jQuery中的window.onload

jQuery中的window.onload,jquery,Jquery,我使用了sizzle的例子,在最后一段代码中我得到了window.onload 这意味着什么 我的代码如下: var init = function () { var trail = Sizzle; var foo = trail('.foo'); foo[0].onmouseover = function () { this.style.backgroundColor = (this.style.backgroundColor.indexOf('blue

我使用了sizzle的例子,在最后一段代码中我得到了window.onload

这意味着什么

我的代码如下:

var init = function () {
    var trail = Sizzle;
    var foo = trail('.foo');
    foo[0].onmouseover = function () {
        this.style.backgroundColor = (this.style.backgroundColor.indexOf('blue') === 0) ? 'yellow' : 'blue';
    };
    foo[1].onmouseover = function () {
        this.style.backgroundColor = (this.style.backgroundColor.indexOf('red') === 0) ? 'yellow' : 'red';
        jQuery(foo[1]).after("<b>Hello</b>");
    }
    foo[2].onmouseover = function () {
        this.style.backgroundColor = (this.style.backgroundColor.indexOf('green') === 0) ? 'yellow' : 'green';
        jQuery(foo[3]).toggle("slow");
        jQuery('b').remove();
    }
};
window.onload = init;
var init=函数(){
var-trail=Sizzle;
var foo=trail('.foo');
foo[0]。onmouseover=函数(){
this.style.backgroundColor=(this.style.backgroundColor.indexOf('blue')==0)?'yellow':'blue';
};
foo[1]。onmouseover=函数(){
this.style.backgroundColor=(this.style.backgroundColor.indexOf('red')==0)?'yellow':'red';
jQuery(foo[1])。在(“Hello”)之后;
}
foo[2]。onmouseover=函数(){
this.style.backgroundColor=(this.style.backgroundColor.indexOf('green')==0)?'yellow':'green';
jQuery(foo[3])。切换(“慢”);
jQuery('b')。remove();
}
};
window.onload=init;

这意味着什么?

这意味着在加载窗口时将执行
函数init

这意味着您正在设置
init
函数来处理文档的
onload
事件。加载页面中的所有内容后,将调用
init
函数

在使用jQuery时,应该改用jQuery事件。DOM事件只能有一个处理程序(除非您编写代码来链接处理程序),但jQuery事件可以有多个处理程序:

$(document).load(init);

如果页面中有多个脚本使用了
onload
事件,则其中一个脚本将替换另一个脚本。即使使用jQuery事件,连接DOM事件的脚本也将接管jQuery事件处理程序,如中所示。在所有脚本中使用jQuery事件可以解决这个问题。

函数init()仅在窗口(页面)加载完成后才开始工作。为什么要进行向下投票?如果你不解释你认为答案有什么问题,它就无法改善它。window.onload≠ document.onload。OP想知道window.onload()的作用,你告诉他使用document.onload。为什么?疯狂的是,你已经知道了!请注意,jQuery 3.0中删除了“load”事件