简体   繁体   English

将事件注册到新添加的选择器

[英]registering event to newly added selector

I am adding and removing items from a ul. 我正在从ul中添加和删除项目。 However, I cannot remove items that were just added. 但是,我无法删除刚刚添加的项目。 I am assuming I need to register the event when the item is added. 我假设添加该项目时需要注册事件。 How to do this please? 请怎么做?

If you are registering an event handler for every li item in your ul , use the .live() function. 如果要为ul每个li项目注册事件处理程序,请使用.live()函数。 This will register the same handler for every new li item that shows up on the page. 这将为页面上显示的每个新li项目注册相同的处理程序。

For example, 例如,

$("li").live("click", function(){
      $(this).remove();
    });

If this doesn't answer your question, could you edit it to be a bit more clear about what you're trying to do? 如果这不能回答您的问题,您可以对其进行编辑以使您对要执行的操作更加清楚吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM