简体   繁体   English

jQuery单击事件删除第一个元素

[英]jQuery click event removing first element

Im working on a project and on my .ejs file I have a popup: 我在一个项目上工作,在我的.ejs文件上,我有一个弹出窗口:

<div id="just-claimed-popup2" class="popup">
   <h6>You just claimed:</h6>
   <h2 id="card-just-claimed"></h2>
   <p class="show-message">Show this Screen!</p>
   <button id="deletePromoFromHome" class="close-button">Close</button>
</div>

On my javascript file I have a code that creates cards on a loop: 在我的javascript文件中,我有一个代码可以在循环中创建卡片:

$('#promotion-container footer').before(`
   <div class="promo card promo${i}">
     <div class="promo-wrapper">
         <div class="promo-header">
            <h2 class="promo-title">${eventName}</h2>
            <span class="close-promo-wrapper"><span class="close-promo"></span></span>
         </div>
   <div class="promo-info">
   <span class="promo-details">
   <p class="promo-detail promo-location">${eventLocation}</p>
   <p class="promo-detail promo-date">${eventDate}</p>
   <p class="promo-detail promo-time">${eventTime}
   <span class="promo-description"></span>
   <span class="buttonRedemp${i}">
        <button class="redddButt load-button2" data="Reedem Card">Reedem Card</button>
    </span>
   </div>
  </div>
</div>
      `)

I want the card to disappear when people click 'redddButt', this is my code: 当人们单击“ redddButt”时,我希望卡消失,这是我的代码:

$(`#promotion-container .promo${i} .redddButt`).on('click', function(e){
    e.stopPropagation();
    $(`div.promo${i}`).addClass('toDelete')

    var esc = $.Event("keyup", { keyCode: 27 });
    $(document).trigger(esc);
    $('#just-claimed-popup2').addClass('reveal');
    $('#card-just-claimed').text(eventName);

    $('#deletePromoFromHome').click(function(){
        $('div.toDelete').fadeOut("slow")
    })
})

PROBLEM: it always removes just the first card clicked and if you click the button in another one it stops working, so it only works once. 问题:它总是只删除单击的第一张卡片,如果您在另一张卡片中单击该按钮,它将停止工作,因此只能工作一次。 If I console.log something the click event is happening, it's just not running the code inside of it. 如果我在console.log中发生了单击事件,则它只是没有在其中运行代码。

Try changing your handler to: 尝试将处理程序更改为:

$('body').on('click', `#promotion-container .promo${i} .redddButt`, function(e){
  //function stuff here
}

The problem might be that elements are generated after the handler is attached. 问题可能在于附加处理程序后会生成元素。

Your code is missing some few closing tags. 您的代码缺少一些结束标记。 Since the cards are dynamically generated, try using (not tested): 由于卡片是动态生成的,请尝试使用(未经测试):

var buttonContext;

$(document).on('click', '#promotion-container .promo .redddButt', function() {
     buttonContext = $(this);
    // Something 
});

$('#deletePromoFromHome').click(function(){
    buttonContext.closest('.promo').fadeOut("slow");
});

You can omit this line: $(div.promo${i}).addClass('toDelete'); 您可以省略以下行: $(div.promo${i}).addClass('toDelete');

The cards may have a single class (.promo) instead of (.promo#), unless may be you want to do further manipulation (say different styling etc). 卡片可能只有一个类(.promo)而不是(.promo#),除非您可能想进行进一步的操作(例如,使用其他样式等)。

Check this for more details on $(document) : https://stackoverflow.com/a/32066793/3906884 $(document)上检查此以获取更多详细信息: https : //stackoverflow.com/a/32066793/3906884

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

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