简体   繁体   English

仅在列表中第一次删除列表中的第一个

[英]Remove first in a list only works first time

I have a list of results, and I want to pop the most recent one with a button click like this: 我有一个结果列表,我想通过单击按钮来弹出最近的结果:

//clear most recent
$('#clear-most-recent').on('click', function () {
  $('#output').children().first().hide(800).done().remove();
});

but it only works the first time. 但只能在第一次使用。 The error is uncaught TypeError, ...is not a function . 错误是uncaught TypeError, ...is not a function I'm guessing each subsequent call tries to access the now-removed element. 我猜每个后续调用都尝试访问现在已删除的元素。

  1. So the handler function isn't chucked out and re-created for each call, right? 因此,不会为每个调用删除并重新创建处理程序功能,对吗?
  2. How can I get the jquery set to update its' contents each time around? 我怎样才能得到设置每次更新其内容的jquery?

link to project on jsbin (be warned, its ugly): https://jsbin.com/lodobaj/edit?html,output 链接到jsbin上的项目(警告,这很丑): https ://jsbin.com/lodobaj/edit?html,输出

Use callback function to remove: 使用回调函数删除:

$('#clear-most-recent').on('click', function () {
   $('#output').children().first().hide(800, function(){
     $(this).remove();
   });
});

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

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