老师 这个click事件怎么不起作用?
来源:3-4 自由编程
慕尼黑4757019
2019-04-11 23:11:47
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box{
width: 200px;
height: 200px;
background: pink;
}
.green{
background: green;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div class="btn">点击这里!</div>
<div class="box">
</div>
<script src="https://cdn.bootcss.com/zepto/1.2.0/zepto.min.js"></script>
<script>
$('.btn').click(function(){
$('.box').wrapInner($('<div class="green"><p>我是</p><p>smallBox内容</p></div>'));
});
$('.green').click(function(){
alert('已执行');
})
</script>
</body>
</html>
1回答
好帮手慕糖
2019-04-12
同学你好,因为这个是动态生成的元素,导致直接绑定事件没有生效哦,建议:可以使用.on绑定事件,例:
希望能帮助到你,欢迎采纳。
祝学习愉快!
相似问题