jQuery不会触发附加项目 [英] jQuery not triggering on appended item
问题描述
我遇到触发事件的问题,这些事件已添加到网页中。在下面的例子中,如果你点击X,它将删除其中一个项目,但是如果你添加一个项目,你不能删除它。
这里是列表:
< ul id =mylist>
< li>项目1< a class =remove> X< / a>< / li>
< li>第2项< a class =remove> X< / a>< / li>
< / ul>
< a class =add>添加< / a>
这里是jQuery:
$('。add')。click(function(){
$('#mylist')。append('< li> Item 3< a class =remove > X< / a>< / li>');
});
$ b $('。remove')。click(function(){
$(this).parent()。hide('slow')。remove();
});
任何人都会对此有所了解,是否需要更新dom或获得jQuery来识别添加元素?
您需要在1.3中使用新添加到jQuery的live API
http://api.jquery.com/live/ p>
如果你不能使用1.3,那么有一个1.2的JQuery插件叫做 LiveQuery ,其工作原理类似。
I'm having a problem triggering events on items that have been added to the page. In the following example, if you click X it will remove one of the items, but you if you add an item you can't remove it.
Here's the list:
<ul id="mylist">
<li>Item 1 <a class="remove">X</a></li>
<li>Item 2 <a class="remove">X</a></li>
</ul>
<a class="add">Add</a>
Here's the jQuery:
$('.add').click(function(){
$('#mylist').append('<li>Item 3 <a class="remove">X</a></li>');
});
$('.remove').click(function(){
$(this).parent().hide('slow').remove();
});
Anyone shed any light on this, do I need to update the dom or something to get jQuery to recognize the appended elements?
You need to use the new live API added to jQuery in 1.3
If you can't use 1.3, there is a JQuery plugin for 1.2 called LiveQuery that works similarly.
这篇关于jQuery不会触发附加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!