简体   繁体   中英

JavaScript - jQuery on click doesn't work correctly?

I have a Sweetalert plugin in use. I made there an table with an X button, after you click button then the sweetalert pops up with table content and the X's. So I made code, if you click on X it will delete it from the table, but it won't work for me.. If I copied the SweetAlert HTML code to JSFiddle and the same deleting code, then it was working.

$('.sweet-container').on('click', 'tr td span', function(){
    $(this).closest('tr').remove();
});

I came across with the similar problem in the past. Your trigger is created before the .sweet-container is created in DOM You can use http://api.jquery.com/delegate/ .

$('.sweet-container').delegate('tr td span', 'click', function(){
    $(this).closest('tr').remove();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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