如何使用 jquery 为动态追加的元素添加点击事件?
How to add click events for dynamically appended elements using jquery?
在这里,我可以在单击时删除 box100 和 box200,但在将其附加到列表后无法删除 box1-box5。我如何 select 添加元素?
https://jsfiddle.net/uhmgj8ky/4/
html
<ul>
<li class='list'>remove box100</li>
<li class='list'>remove box200</li>
</ul>
<div class='box' box='box1'>add box1</div>
<div class='box' box='box2'>add box2</div>
<div class='box' box='box3'>add box3</div>
<div class='box' box='box4'>add box4</div>
<div class='box' box='box5'>add box5</div>
css
.box{
height:50px;
width:50px;
border:2px solid blue;
display: inline-block;
}
ul li{
height:50px;
width:50px;
border:2px solid blue;
display: inline-block;
margin-right: 1px;
}
jquery
$('.box').on('click', function(){
var txt = $(this).attr('box');
$('ul').append("<li class='list'>remove "+txt+"</li>");
});
$('.list').on('click', function(){
$(this).remove();
});
你应该使用事件委托
Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future. Both
on()
and
delegate()
functions allow us to perform event delegation.
添加了一个容器 class
<ul>
<li class='list'>remove box100</li>
<li class='list'>remove box200</li>
</ul>
<div class="box-container">
<div class='box' box='box1'>add box1</div>
<div class='box' box='box2'>add box2</div>
<div class='box' box='box3'>add box3</div>
<div class='box' box='box4'>add box4</div>
<div class='box' box='box5'>add box5</div>
</div>
$('.box-container').on('click',".box", function(){
var txt = $(this).attr('box');
$('ul').append("<li class='list'>remove "+txt+"</li>");
});
$('ul').on('click','.list', function(){
$(this).remove();
});
更新
https://jsfiddle.net/uhmgj8ky/6/
有关事件委托的更多信息,see video
在这里,我可以在单击时删除 box100 和 box200,但在将其附加到列表后无法删除 box1-box5。我如何 select 添加元素?
https://jsfiddle.net/uhmgj8ky/4/
html
<ul>
<li class='list'>remove box100</li>
<li class='list'>remove box200</li>
</ul>
<div class='box' box='box1'>add box1</div>
<div class='box' box='box2'>add box2</div>
<div class='box' box='box3'>add box3</div>
<div class='box' box='box4'>add box4</div>
<div class='box' box='box5'>add box5</div>
css
.box{
height:50px;
width:50px;
border:2px solid blue;
display: inline-block;
}
ul li{
height:50px;
width:50px;
border:2px solid blue;
display: inline-block;
margin-right: 1px;
}
jquery
$('.box').on('click', function(){
var txt = $(this).attr('box');
$('ul').append("<li class='list'>remove "+txt+"</li>");
});
$('.list').on('click', function(){
$(this).remove();
});
你应该使用事件委托
Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future. Both
on()
and
delegate()
functions allow us to perform event delegation.
添加了一个容器 class
<ul>
<li class='list'>remove box100</li>
<li class='list'>remove box200</li>
</ul>
<div class="box-container">
<div class='box' box='box1'>add box1</div>
<div class='box' box='box2'>add box2</div>
<div class='box' box='box3'>add box3</div>
<div class='box' box='box4'>add box4</div>
<div class='box' box='box5'>add box5</div>
</div>
$('.box-container').on('click',".box", function(){
var txt = $(this).attr('box');
$('ul').append("<li class='list'>remove "+txt+"</li>");
});
$('ul').on('click','.list', function(){
$(this).remove();
});
更新 https://jsfiddle.net/uhmgj8ky/6/
有关事件委托的更多信息,see video