1、之前用的是jq1.4.3 ,jq1.7一下都可以使用live()方法,来实现该功能
?
1 2 3 | $(‘ #div').live(‘click',function(){ }); |
但是live方法也有不支持的事件,例如:toggle事件 ,遇到这个情况可以给他加个click事件,之后再来个模拟点击trigger事件就ok了
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $( 'a' ).live( 'click' , function (){ $( this ).toggle( function (){ alert( "q11" ); alert($( this ).attr( "id" )); $( this ).parent().children( 'ul' ).show(); }, function (){ $( this ).parent().children( 'ul' ).hide(); }); $( this ).trigger( 'click' ); }); |
2、jq1.7以上的就用on方法了,第一个属性为事件,第二个是 选择器,第三个是 执行的方法
?
1 2 3 | $(document).on( "click" , "#d1" , function (){ alert( "bbbbb" ); }); |