简体   繁体   English

附加元素后,事件单击不适用于 jquery 中的附加元素

[英]After appending an element the event click is not working on the appended element in jquery

I am appending item by Jquery.我正在通过 Jquery 附加项目。 But after appending can't bind the event on the appended item.但是在附加后不能在附加项上绑定事件。 I am appending as follows >>我附加如下>>

var item =  '<div id="'+newInputId+'" class="col-md-9" style="padding-right: 0px;">';
    item += '<input id="txtInScope" type="text" value="'+currentScopeVal+'" class="form-control" readonly="readonly"/>';
    item += '</div>';
    item += '<div id="inScopeActionDiv'+newInputId+'" class="col-md-3"  style="padding-left: 2px;">';
    item += '<button type="button" class="btn btn-warning btn-sm remButton" title="Remove this item">Remove Item</button>';
    item += '</div>';    
$('#inScopeDiv').append(item);

And after appending this I want to bind a click event on the above remButton class as below >>在附加这个之后,我想在上面的 remButton 类上绑定一个点击事件,如下所示 >>

$("#inScopeDiv").delegate(".remButton", "click", function(){
    alert('you clicked me again!');
});
$('#inScopeDiv').on('click', '.remButton', function() {
    alert("working");
})
$('.remButton').live('click', function() {
    alert('live');
})

But no result.但没有结果。 Can anyone please help me on this please?任何人都可以请帮助我吗?

将它绑定到一个非动态但始终在 DOM 中的父级上。

$('.remButton').live('click', function() {
    alert('live');
})

jquery method live is not valid anymore: jquery 方法 live 不再有效:

"As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live()." “从 jQuery 1.7 开始,.live() 方法已被弃用。使用 .on() 附加事件处理程序。旧版本的 jQuery 用户应优先使用 .delegate() 而不是 .live()。”

Source: jquery live来源: jquery live

Little explanation about event attachment:关于事件附件的小说明:

You must realize that a target what you want to add a event, exists BEFORE to call the add event function(in this case with the method on of jQuery).您必须意识到要添加事件的目标在调用添加事件函数之前存在(在这种情况下使用 jQuery 的方法)。

on another hand, exists with jquery a manner to make work a event attachment without the existence of the element before:另一方面,与 jquery 一起存在的方式是使工作成为事件附件,而之前不存在元素:

$('html').on('click', '#inScopeDiv .remButton', function () {
  alert('works!');
});

You need to add the listener each time you add an item:每次添加项目时都需要添加侦听器:

$('#inScopeDiv').append(item)
            .off() //unbind old listeners so no duplicate listeners
            .on('click', '.remButton', function() {
               alert("working");
            });

You could store the appended div in a variable using .appendTo and then you could attach the click event directly to the variable.您可以使用.appendTo将附加的 div 存储在变量中,然后您可以将点击事件直接附加到变量。 See it working: JSFiddle看看它是否有效: JSFiddle

$(".appendDiv").click(function () {
      var item = "<div>I'm a new div!</div>";
      var appended_div = $(item).appendTo(".container");
      appended_div.click(function () {
              alert("Working!");
      });
});

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

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