支持给动态元素和属性绑定事件的是live和on,其中live在JQUERY 1.7之后就不推荐使用了。现在主要用on,使用on的时候也要注意,on前面的元素也必须在页面加载的时候就存在于dom里面。动态的元素或者样式等,可以放在on的第二个参数里面。
案例如下:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/jquery/2.2.4/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#btn").click(function(){
$('#div1').append('<button>newAdd</button>')
});
$("#div1").on("click",'button',function(){
alert("click");
});
});
</script>
</head>
<body>
<div id="div1">
</div>
<button>abc</button>
<input type="button" id='btn' value='add'/>
</body>
</html>