当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Jquery事件绑定实例学习之动态绑定和静态绑定

Jquery事件绑定实例学习之动态绑定和静态绑定

2018年11月13日  | 移动技术网IT编程  | 我要评论

jquery 事件绑定---动态绑定和静态绑定

1.静态元素绑定:

方法一:

[html] view plain copy

<span style="font-size:14px;">$('#btn_submit').click(function(){  

});</span>  

方法二:直接在button标签中使用onclick绑定

[html] view plain copy

<span style="font-size:16px;"><button type="submit" id="btn_submit" onclick="btnaction()"> submit </button></span>  

方法三:使用bind函数

[html] view plain copy

<span style="font-size:16px;">$('#btn').bind("click",function(){  

    omething();  

})</span>  

方法四:使用on函数

[html] view plain copy

<span style="font-size:16px;">$('#btn').on("click",function(){  

    dosomething();  

})  

</span>  

动态元素绑定:

1.例子1:

[html] view plain copy

$("table").on("click","input[name='result']",function(){  

     var len = $(this).parent().siblings().children("input").attr("checked",false);  

     $(this).attr("checked",true);  

});  

注意:#btn是动态后添加进去的,#datatable必须是页面中本有的,是静态的,且#btn在#datatable范围内

2.例子2:

 $("table").on("click","input[name='result']",function(){

      var len = $(this).parent().siblings().children("input").attr("checked",false);

      $(this).attr("checked",true);

        

 });

$('table') 是页面已经存在的。

$('input[name='result']')是动态加载的元素。

绑定点击事件。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网