我使用了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;
Run Code Online (Sandbox Code Playgroud)
这是什么意思?
这意味着您正在设置init处理onload文档事件的功能.init当页面中的所有内容都已加载时,将调用该函数.
当您使用jQuery时,您应该使用jQuery事件.DOM事件只能有一个处理程序(除非你编写代码来链接处理程序),但jQuery事件可以有多个处理程序:
$(document).load(init);
Run Code Online (Sandbox Code Playgroud)
如果页面中有多个使用该onload事件的脚本,则会替换另一个脚本.即使您使用jQuery事件,连接DOM事件的脚本也将接管jQuery事件处理程序,就像在这个问题中一样.在所有脚本中使用jQuery事件解决了这个问题.
| 归档时间: |
|
| 查看次数: |
24405 次 |
| 最近记录: |