简体   繁体   English

触发器不适用于在.change jQuery上添加的动态内容

[英]Trigger doesn’t work for dynamic content added on .change jQuery

I append some HTML dynamically, once an <input> field has changed. 一旦更改了<input>字段,我会动态添加一些HTML。 When I click on <a href="" class="js-remove-item"> link I want to remove that item. 当我单击<a href="" class="js-remove-item">链接时,我想删除该项目。 Yet the trigger for that link doesn't happen. 但是,该链接的触发没有发生。

I tried adding the class name .tvshow of the <input> to the data handler like so: 我尝试将<input>的类名.tvshow添加到数据处理程序中,如下所示:

From this: 由此:

$('.js-remove-item').on('click touchstart', function() {

to this: 对此:

$('.js-remove-item').on('click touchstart', '.tvshow', function() {

Yet, it's not working. 但是,它不起作用。 What is going on? 到底是怎么回事?

Check the demo: http://jsfiddle.net/7tGCh/ (try typing something in the input and blur it, then click on link to remove that item) 查看演示: http : //jsfiddle.net/7tGCh/ (尝试在输入中输入内容并对其进行模糊处理,然后单击链接以删除该项目)

The element is inserted dynamically, so you'll need to delegate the event : 该元素是动态插入的,因此您需要委派事件:

$('.container__list-of-movies').on('click touchstart', '.js-remove-item', function() {
  $(this).parent().addClass('remove-tv-show');
  window.setTimeout(function() {$('.remove-tv-show').remove();}, 500);

  alert('removing');
  return false
})

FIDDLE 小提琴

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

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