简体   繁体   中英

Javascript delete html table row

I dynamically create an html table and one of the cells in each row stores a button that I want to delete that row when pressed. What are my options in knowing what table row to delete from the delete button pressed?

Am I somehow able to get the row that the button is in? Maybe then inside the click I could use the events 'this' property to get the button and then find out which cell it's in, and from there which row that cell is in? Not sure how I'd go about doing that though.

Can be done with native JS, but here is a jQuery solution. Be sure to clone(true) to ensure your events are preserved.

<tr><td>hello</td><td><span class="deleteMe">Delete</td></tr>

$(".deleteMe").click(function(){$(this).parent().parent().remove();});

将其放入按钮的onclick处理程序中:

this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);

if you are using jQuery this will be a sample code on how you can achieve this.

$('#my_table_id').on('click', 'button', function() {
    $(this).closest('tr').remove();
});

hope this helps, best!

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