简体   繁体   English

附加元素单击功能无法按预期工作?

[英]Appended element click function is not working as expected?

I attach click event to appended element by $('body').on('click') . 我通过$('body').on('click')将click事件附加到附加元素$('body').on('click') But if I click id one two times, it also alert two times when its appended element click. 但是,如果我单击id两次,它的附加元素单击时也会发出两次警报。

If I attach like this $('body').off().on("click") , the last appended element click can alert only (if I click id one first and then click id two,then id one appended element click can't alert) . 如果我这样附加$('body').off().on("click") ,则最后一个附加元素的单击只能$('body').off().on("click")警报(如果我先单击id一个,然后单击id两个,则再附加一个id,单击无法提醒)

How can I attach click event for every appended element to alert once ? 如何为每个附加元素附加click事件以发出警报?

 $(document).ready(function(){ test.init(); }) var test = { init : function() { $("#one").on("click",function(){ test.appendOne(); }); $("#two").on("click",function(){ test.appendTwo(); }); }, appendOne: function() { $("<button class='one-after'>").append("<h1>One</h1>").appendTo("body"); $('body').on("click",'.one-after',function() { alert("clickedOne"); }); }, appendTwo: function() { $("<button class='two-after'>").append("<h1>Two</h1>").appendTo("body"); $('body').on("click",'.two-after',function() { alert("clickedTwo"); }); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="one">One</button> <button id="two">Two</button> 

You should use selector children() which is a selector on every first level children of the body element (see documentation reference at https://api.jquery.com/children/ ) 您应该使用选择器children(),它是body元素的每个第一级子代的选择器(请参阅https://api.jquery.com/children/上的文档参考)

So your code would become : 因此您的代码将变为:

$('body').children().on("click")

with a slight modification, add the event directly to the appended object as below: 稍作修改,将事件直接添加到附加对象中,如下所示:

  $("<button class='one-after'>").append("<h1>One</h1>").appendTo("body")    
        .on("click",function() {
          alert("clickedOne"); })

you can repeat this pattern for the other one 您可以为另一个模式重复此模式

That happen since every time you click the button, you bind a new event on the body . 发生这种情况是因为每次您单击按钮时,都会在body上绑定一个新事件。

You already are using event delegation, you only have to bind the event once and every new element will have an event "attached" to it. 您已经在使用事件委托,您只需绑定一次事件,每个新元素都将有一个“附加”的事件。 Move your event binding inside your init : 将事件绑定init

 $(document).ready(function(){ test.init(); }) var test = { init : function() { $('body').on("click",'.one-after',function() { alert("clickedOne"); }).on("click",'.two-after',function() { alert("clickedTwo"); }); $("#one").on("click",function(){ test.appendOne(); }); $("#two").on("click",function(){ test.appendTwo(); }); }, appendOne: function() { $("<button class='one-after'>").append("<h1>One</h1>").appendTo("body"); }, appendTwo: function() { $("<button class='two-after'>").append("<h1>Two</h1>").appendTo("body"); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="one">One</button> <button id="two">Two</button> 

You may simply change these two lines: 您可以简单地更改这两行:

$("<button class='one-after'>").append("<h1>One</h1>").appendTo("body");    
$('body').on("click",'.one-after',function() {

to (chain appendTo with on. Remove the event delegation): 到(链接到appendTo并打开。删除事件委托):

$("<button class='one-after'>").append("<h1>One</h1>").appendTo("body").on("click",function() {

Instead of creating a delegation click event handler on the body you can simply add the click event to each element. 无需在主体上创建委派click事件处理程序,您只需将click事件添加到每个元素即可。

 $(document).ready(function(){ test.init(); }) var test = { init : function() { $("#one").on("click",function(){ test.appendOne(); }); $("#two").on("click",function(){ test.appendTwo(); }); }, appendOne: function() { $("<button class='one-after'>").append("<h1>One</h1>").appendTo("body").on("click",function() { alert("clickedOne"); }); }, appendTwo: function() { $("<button class='two-after'>").append("<h1>Two</h1>").appendTo("body").on("click",function() { alert("clickedTwo"); }); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="one">One</button> <button id="two">Two</button> 

You can use jQuery's .one() variant: 您可以使用jQuery的.one()变体:

$("#one").one("click",function(){
  test.appendOne();
});

This will fire only once. 这只会触发一次。 http://api.jquery.com/one/ http://api.jquery.com/one/

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

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