jQuery mouseover mouseout事件在IE下閃爍的解決方法
$("#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(); } ); } );
瀏覽器之間的不兼容一直令前端開發者的頭疼,而 IE 更是噩夢。鼠標在下拉菜單移動時菜單會不斷閃爍,tb說明不斷觸發了 mouseover 和 mouseout 事件。
這貌似涉及到所謂的“事件冒泡”,我不懂 JavaScript,就不在誤人子弟了,詳情請自己 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(); } ); } );
最后指出一點,mouseenter 和 mouseleave 事件是 jQuery 庫中實現的,似乎(再次聲明我不懂 JavaScript,所以這里用“似乎”)并不是瀏覽器的原生事件,所以如果你想自己寫 JavaScript 實現的話,請自行 Google,或者查看 jQuery 源碼,如果你能看懂的話。
參考鏈接:JQuery HowTo: Problems with jQuery mouseover / mouseout events
轉自:http://demon.tw/programming/jquery-mouseover-mouseout.html