我试图动态地将列表项目添加到列表中,然后通过单击该项目将其删除。移除事件似乎从未触发。但是,如果我在静态列表上使用相同的代码,它工作正常。我见过类似的解决方案,但他们不工作。用jQuery删除动态添加的LI列表项目
见的jsfiddle: http://jsfiddle.net/Lc2bC/
<input id="status" type="text" placeholder="Status (tab or enter)">
<br/>
Dynamic List:
<ul id="statusList"></ul>
$('#status').keydown(function (e) {
if (e.which == 9 || e.which == 13) {
$('#statusList').html($('#statusList').html() + formatNewStatus(this.value));
this.value = "";
this.focus();
e.preventDefault();
}
});
$(function() {
$('#statusList li').click(function() {
alert('dynamicList');
$(this).remove();
})
});
你必须使用委托与() –