如何将jQuery事件绑定到通过其他事件生成的元素上?
使用jQueryon()方法将jQuery事件绑定到通过其他事件生成的元素上。您可以尝试运行以下代码,以了解如何使用on()方法绑定jQuery事件:
示例
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).on({
mouseenter: function (e) {
alert('mouse enter');
},
mouseleave: function (e) {
alert('mouse leave');
}
}, '.demo');
});
</script>
</head>
<body>
<h1>Heading 1</h1>
<div class="demo" id="d1">Demo One</div>
<div class="demo" id="d2">Demo Two</div>
<p>Mouse enter and leave will generate alert boxes.</p>
</body>
</html>