简体   繁体   English

.on()在附加元素上不起作用(jQuery)

[英].on() doesn't work on appended element(jQuery)

So I'm appending a row in a table and then upon clicking a button I want to remove that row. 所以我要在表格中追加一行,然后在单击按钮时要删除该行。 However I can't do it in a way that seems natural, like: 但是我不能以一种看起来自然的方式来做到这一点,例如:

$(".usun").on("click", function(){
    $(this).parent().parent().remove();
});

where .usun is a class of buttons that remove a row in a table. 其中.usun是一类按钮,用于删除表中的一行。 Of course this fragment of code is inside of $(document).ready(... . What seems to work for me is this: 当然,这段代码片段位于$(document).ready(... 。对我来说似乎$(document).ready(...是:

$(this).on("click", ".usun", function(){ 
    $(this).parent().parent().remove();
});

and I don't understand why. 我不明白为什么。 Could someone explain me this? 有人可以解释一下吗?

There are several method signatures available for .on() which you can read about here . .on()有几种方法签名,您可以在此处阅读。

Your first syntax ( $(".usun").on( ) is, just one time, attaching click handlers to each individual .usun element. If more are added later, they don't have click handlers. 您的第一个语法( $(".usun").on( )只是一次,将单击处理程序附加到每个单独的.usun元素。如果以后添加更多,则它们没有单击处理程序。

The second syntax ( $(this).on("click", ".usun", function()... ) attaches a single listener to the document , saying "any time a .usun inside me is clicked, do something". This covers a case where more matching elements are added after $(document).ready is fired. 第二种语法( $(this).on("click", ".usun", function()... )将单个侦听器附加到document ,表示“ .usun单击我体内的.usun ,请执行任何操作”这涵盖了在触发$(document).ready之后添加更多匹配元素的情况。

That is because that specific .usun does not exist yet when you are registering the event. 这是因为在注册事件时,尚不存在该特定的.usun You need to register the event to its parent element (prefereably the table id) like so: 您需要将事件注册到其父元素(最好是表ID),如下所示:

$('#yourtableid').on("click", ".usun", function(){ //if table id does not exist, you can use $('body') instead
    $(this).parent().parent().remove();
});

This will make sure that you are attaching the events even to 'unborn' elements. 这将确保您将事件甚至附加到“未出生”的元素上。

Hope this helps! 希望这可以帮助!

So you're aware of this... your question is related to live and non-live lists (collections) in javascript; 因此,您意识到了这一点...您的问题与javascript中的实时非实时列表 (集合)有关;

review the following Javascript, 查看以下Javascript,

var queryStatic = document.querySelectorAll(".usun"); //non-live | static list
var queryLive = document.getElementsByTagName("div"); //live list

queryStatic will return a NodeList that statically references the nodes that satistifed the condition of the query. queryStatic将返回一个NodeList,该List静态引用满足查询条件的节点。

If you were to alter/modify the properties/fields/members of a node or it's element that is referenced by this NodeList outside of using the queryStatic, it would be represented in the queryStatic instance; 如果要在不使用queryStatic的情况下更改/修改此NodeList引用的节点或其元素的属性/字段/成员,则将在queryStatic实例中表示该元素;

you cannot, however, add a new element/node to the DOM that would have satisfied the query and expect it to be in that NodeList. 但是,您不能将新的元素/节点添加到DOM中,该元素/节点满足查询并期望其位于该NodeList中。

queryList (an HTML Collection), however, will represent both aspects. 但是,queryList(HTML集合)将代表这两个方面。 It is a live list. 这是一个实时列表。

The point is, the underline concept has little to nothing to do with method signatures. 重点是,下划线概念与方法签名几乎没有关系。

Edit 编辑

However, in the case of jQuery's .on() method; 但是,就jQuery的.on()方法而言; there is support for attaching the listener to a parent element (like Document ) and specifying an additional query criteria. 支持将侦听器附加到父元素(如Document )并指定其他查询条件。

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

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