1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 动态新增元素的js无效的解决方法

动态新增元素的js无效的解决方法

时间:2024-05-10 00:34:35

相关推荐

动态新增元素的js无效的解决方法

通过监听父级容器下匹配子元素进行事件绑定

<divid="test1"><inputtype="button"class="btn"value="按钮1"/><inputtype="button"class="btn"value="按钮1"/><inputtype="button"class="btn"value="按钮1"/><p></p></div><inputtype="button"class="add"value="加一个按钮"/><scripttype="text/javascript"src="js/jquery-1.10.2.min.js"></script><scripttype="application/javascript">$(function(){$('#test1').on('click','.btn',function(){alert('绑定有效1')});$('.add').on('click',function(){$('#test1').append('<inputtype="button"class="btn"value="按钮2"/>')});});</script>

$('.add').on('click',function(){$('#test1').append('<inputtype="button"class="btn"value="按钮2"/>');$('.btn').on('click',function(){});});

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。