動態生成的元素做事件委託
正確寫法:
$(document.body).on('mouseenter',"#newslist li:not(:first)",function(e){
$(this).css("background-color","#EC4413");
$(this).children().css("color","#ffffff");
});
$(document.body).on('mouseout',"#newslist li:not(:first)",function(e){
$(this).css("background-color","#eeeeee");
$(this).children().css("color","#333333");
});
錯誤寫法:
$("#newslist li:not(:first)").hover(
function(){
$(this).css("background-color","#EC4413");
$(this).children().css("color","#ffffff");
},
function(){
$(this).css("background-color","#eeeeee");
$(this).children().css("color","#333333");
}
);