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

javascript - jQuery addClass only to <td> where the parent <tr> has specific class - Stack Overflow

programmeradmin3浏览0评论

so what i try to do is, to add a class to every element, where the parent has a specific class.

I tried this:

if ($('tr').hasClass('storno')) {
        $('tr > td').addClass('storno');
    }

and this is the html example:

<table>
  <tr class="storno">
    <td>test</td>
    <td>test2</td>
  </tr>
  <tr>
    <td>test</td>
    <td>test2</td>
  </tr>
</table>

i tried it also with some other code, but at this point, i dont know i can get it right.

Thank you very much in advance

so what i try to do is, to add a class to every element, where the parent has a specific class.

I tried this:

if ($('tr').hasClass('storno')) {
        $('tr > td').addClass('storno');
    }

and this is the html example:

<table>
  <tr class="storno">
    <td>test</td>
    <td>test2</td>
  </tr>
  <tr>
    <td>test</td>
    <td>test2</td>
  </tr>
</table>

i tried it also with some other code, but at this point, i dont know i can get it right.

Thank you very much in advance

Share Improve this question asked Jul 11, 2013 at 10:00 xKevinxKevin 531 silver badge5 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 12
$('tr.storno > td').addClass('storno');

you need to use the class selector along with descendent selector to do this

$('tr.storno td').addClass('storno');

Demo: Fiddle

Do this

$('tr.storno > td').addClass('storno');

simply use a selector with the specific class when you fetch the nodes:

$('tr.specificParentClass').children('td').addClass('tdClass');

Here, try this one

$('tr.storno').find('td').addClass('storno');
if ($('tr').hasClass('storno')) {
    $(this).find('td').addClass('storno');
}
发布评论

评论列表(0)

  1. 暂无评论