$("#category ul").find("li").each(
function() {
$(this).mouseover(
function() {
$("#" + this.id + "_menu").show();
$(this).addClass("a");
}
);
$(this).mouseout(
function() {
$(this).removeClass("a");
$("#" + this.id + "_menu").hide();
}
);
}
);
瀏覽器之間的不兼容一直令前端開(kāi)發(fā)者的頭疼,而 IE 更是噩夢(mèng)。鼠標(biāo)在下拉菜單移動(dòng)時(shí)菜單會(huì)不斷閃爍,tb說(shuō)明不斷觸發(fā)了 mouseover 和 mouseout 事件。
這貌似涉及到所謂的“事件冒泡”,我不懂 JavaScript,就不在誤人子弟了,詳情請(qǐng)自己 Google,這里只給出解決方法:將 mouseover 改成 mouseenter,mouseout 改成 mouseleave。
$("#category ul").find("li").each(
function() {
$(this).mouseenter(
function() {
$("#" + this.id + "_menu").show();
$(this).addClass("a");
}
);
$(this).mouseleave(
function() {
$(this).removeClass("a");
$("#" + this.id + "_menu").hide();
}
);
}
);
最后指出一點(diǎn),mouseenter 和 mouseleave 事件是 jQuery 庫(kù)中實(shí)現(xiàn)的,似乎(再次聲明我不懂 JavaScript,所以這里用“似乎”)并不是瀏覽器的原生事件,所以如果你想自己寫(xiě) JavaScript 實(shí)現(xiàn)的話(huà),請(qǐng)自行 Google,或者查看 jQuery 源碼,如果你能看懂的話(huà)。
參考鏈接:JQuery HowTo: Problems with jQuery mouseover / mouseout events
轉(zhuǎn)自:http://demon.tw/programming/jquery-mouseover-mouseout.html