A-A+
jquery动态生成元素鼠标响应方法live()函数
在jquery中live() 方法为被选元素附加一个或多个事件处理程序,并规定当这些事件发生时运行的函数了,下文就来介绍在我们动态创建事件元素时再绑定live()方法,下文就一起来看看吧。
最简单的方法,当点击按钮时,隐藏或显示 p 元素,代码如下:
- $("button").live("click",function(){
- $("p").slideToggle();
- });
jQuery在动态生成HTML元素后,如果该元素集合中有鼠标点击CLICK事件。
这时点击无响应,需要用到.live()方法使生成的动态元素依然保持页面装载后的效果,使鼠标点击事件生效,代码如下:
- jQuery(document).ready(function(){
- var contentTR=$('.info tr:eq(0)').html();
- var e=0;
- $('.addrow').click(function(){
- e=e+1;
- $(".info .addrow").parent().parent().before('<tr>' + contentTR + '</tr>');
- $(".info tr:eq("+e+")").find("input:eq(0)").attr("name","a"+e);
- $(".info tr:eq("+e+")").find("input:eq(1)").attr("name","b"+e);
- $(".info tr:eq("+e+")").find("select:eq(0)").attr("name","c"+e);
- $(".info tr:eq("+e+")").find("input:eq(2)").attr("name","d"+e);
- $(".info tr:eq("+e+")").find("input:eq(3)").attr("name","f"+e);
- });
- $('.del').live('click',function(){
- $(this).parent().parent().remove();
- });
- });