简体   繁体   English

将行动态添加到表后,jQuery选择器未触发

[英]jQuery selector not firing after dynamically adding a row to a table

I have a table as 我有一张桌子

<table id="table1">
   <tr>
      <td>Cell 1</td>
      <td>Cell2</td>
   </tr>
</table>

On selecting a cell 在选择单元格时

$('#table1 tr td').bind('click', function(){
     alert('Selected');
})

However, when I add a cell dynamically 但是,当我动态添加单元格时

$(this.parentNode).after('<tr><td>Cell 3</td><td>Cell 4</td></tr>');

Now, when I click the Cell 3 or Cell 4, the alert message is not fired. 现在,当我单击单元格3或单元格4时,不会触发alert消息。

How can this be fixed without refreshing the page? 如何在不刷新页面的情况下解决此问题?

User jquery "on" method instead of bind. 用户jquery的"on"方法代替绑定。 https://api.jquery.com/on/ It's main dif from bind is that it performs kind of watch for element in selector and add 'live bind'. https://api.jquery.com/on/与bind的主要区别是它对选择器中的元素执行某种监视并添加“实时绑定”。 Here is the sample: http://jsbin.com/yapiheju/1/edit 这是示例: http : //jsbin.com/yapiheju/1/edit

You will have to reassign the event listeners to the new DOM nodes too. 您还必须将事件侦听器重新分配给新的DOM节点。 Right after dinamically adding a cell, make sure you set a listener on it too. 在动态添加一个单元格之后,请确保您也对其设置了一个侦听器。

jQuery docs: Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on(). jQuery docs: Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on(). Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on().

Or you could use delegated events along with jQuery's on , as other users suggested. 或者,您也可以按照其他用户的建议,将委托事件与jQuery的on一起使用。 All you have to do is to figure out which element that is closer to the core document in the DOM than your cells are, is surely to be present as a real node by the time you attach the events. 您要做的就是找出在DOM中比单元更接近核心文档的元素,并确保在附加事件时将其作为真实节点显示。 In your case, it would be best to choose the document itself as you will not need to wait for the whole DOM to load. 在您的情况下,最好选择document本身,因为您无需等待整个DOM加载。

Now, just set the click event to the document . 现在,只需将click事件设置为document So everytime a click event will get to the document (by directly clicking it or by bubbling to it), the callback will be fired. 因此,每次单击事件到达document (通过直接单击或冒泡到document )时,都会触发回调。 But we don't want that. 但是我们不想要那样。 Well, it's simple. 好吧,很简单。 Just pass one more argument to the .on() method, between the event name and the callback. 只需在事件名称和回调之间传递一个参数给.on()方法即可。 This argument will be a selector string pointing to the descendants which will be allowed to bubble this specific event up to the document . 此参数将是一个选择器字符串,指向后代,该后代将允许将此特定事件冒泡到document

Use the jquery on on使用jQuery

eg. 例如。

$(document).on("click", 
    "#table1 tr td", function(){ 
    alert('Selected'); 
}); 

Documentation here: http://api.jquery.com/on/ 此处的文档: http : //api.jquery.com/on/

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

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