最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery select closest tr with specified class - Stack Overflow

programmeradmin2浏览0评论

I have a list of items inside a table. I have decided to change the table structure a bit so that things look more organized, and added some more <tr>'s in. What happened is that my jQuery code doesn't work well anymore.

I have that piece of code:

$(':input:not(.ajax-confirmed)').each(function(m,r){
    var $row = $(r).closest('tr');
    $row.remove();
});

I would simply like to select the closest <tr> and signify it with an additional parameter, I thought maybe a class, so that I know what kind of <tr> I am removing.

How can I select the closest tr with a specific class name?

I have a list of items inside a table. I have decided to change the table structure a bit so that things look more organized, and added some more <tr>'s in. What happened is that my jQuery code doesn't work well anymore.

I have that piece of code:

$(':input:not(.ajax-confirmed)').each(function(m,r){
    var $row = $(r).closest('tr');
    $row.remove();
});

I would simply like to select the closest <tr> and signify it with an additional parameter, I thought maybe a class, so that I know what kind of <tr> I am removing.

How can I select the closest tr with a specific class name?

Share Improve this question edited Oct 9, 2011 at 12:44 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Oct 9, 2011 at 12:42 TedTed 3,87315 gold badges59 silver badges100 bronze badges 1
  • Show your relevant HTML structure. – Rob W Commented Oct 9, 2011 at 12:45
Add a ment  | 

3 Answers 3

Reset to default 3

This should work, depending on your HTML structure:

$(':input:not(.ajax-confirmed)').each(function(m,r){
    var $row = $(r).closest('tr.className');
    $row.remove();
});

Hope this helps!

If you look in the documentation, you will find this example:

$('li.item-a').closest('ul', listItemII)

.css('background-color', 'red');

The second parameter coult be a class name. So in your example, just add something like this:

var $row = $(r).closest('tr', 'some class name');

"I would simply like to select the closest "

.closest('tr')

or

.closest('.myclass')
发布评论

评论列表(0)

  1. 暂无评论