1.需求:常常要动态加载dom节点,加载以后的节点固然也会有一些绑定事件的需求,今天给一个事件绑定hover,用jQuery,居然没有生效。先上没有生效的代码 css
$('ul.course_lists').on('hover','li',function(){ // mouseenter dosomething },function(){ // mouseleave dosomething });
2.问题出在哪里了?dom
$(selector).on(event,childSelector,data,function,map)
3.分析函数
在JQuery中,hover()函数自己是对 mouseenter && mouseleave 的封装,然而在原生event中,并无hover这一事件,因此在传递参数hover时,并不会有任何的效果。this
若是我想绑定相似hover的效果,能够绑定两个事件mouseenter && mouseleave + 一个function ,样式就用类名来toggle,也能够在map里面,一个事件对应一个functionspa
4.代码来了code
// js
$('ul.course_lists').on('mouseenter mouseleave','li',function(){ $(this).toogleClass('border_color'); }); // css .border_color{ border-color:#ccc; } li{ border:1px solid #fff; }
$('ul.course_lists li').on({ mouseenter:function(){ $(this).css('border-color','#ccc'); }, mouseleave:function(){ $(this).css('border-color','#fff'); } });
5.搞定,but,on()方法不要再出错了! blog