简体   繁体   English

jQuery的最后一个选择器不“刷新”

[英]jQuery last-child selector not “refreshing”

On the last textarea box on the page a function is called to insert another textarea box. 在页面上的最后一个textarea框上,调用一个函数以插入另一个textarea框。 Making it the new last textarea, however jQuery does not see this textarea being the new last textarea. 使其成为新的最后一个文本区域,但是jQuery并未将此文本区域视为新的最后一个文本区域。

The function sticks on the last textarea that was inserted via html. 该函数粘贴在通过html插入的最后一个文本区域上。

 function add_new() { $('.edit_main').append('<li><div class="edit_one"><textarea class="text1"></textarea></div><div class="edit_two"><textarea class="text2"></textarea><textarea class="text3"></textarea></div><div style="clear:both;"></div></li>'); } $(".edit_main li:last-child div.edit_two textarea:last-child").keydown(function(e) { var code = e.keyCode || e.which; if (code == 9) { add_new(); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ol class="edit_main"> <li> <div class="edit_one"> <textarea class="text1"></textarea> </div> <div class="edit_two"> <textarea class="text2"></textarea> <textarea class="text3"></textarea> </div> <div style="clear:both;"></div> </li> <li> <div class="edit_one"> <textarea class="text1"></textarea> </div> <div class="edit_two"> <textarea class="text2"></textarea> <textarea class="text3"></textarea> </div> <div style="clear:both;"></div> </li> </ol> 

jsfiddle jsfiddle

This is a one-time selector. 这是一次性选择器。 In order to get it to dynamically update, you need to use event delegation . 为了使其能够动态更新,您需要使用事件委托

$(document).on("keydown", ".edit_main li:last-child div.edit_two textarea:last-child", function (e) {
  if (e.which == 9){
    add_card();
  }
});

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

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