当前位置: 动力学知识库 > 问答 > 编程问答 >

Traversing HTML using Jquery

问题描述:

My sample HTML

<tr>

<td>

<div><span id="temp" />

</div>

</td>

</tr>

<tr>

<td>

<div><span id="temp" />

</div>

</td>

</tr>

From the Span tag how do i get the Table tr tag and set its background color ?

Any easy methods rather than finding parent.parent?

And only the particular span tag TR should be changed not all the table TR ?

Thanks

网友答案:

you can use .closest()

$('#temp').closest('tr').css('background-color', 'green');
  • http://api.jquery.com/closest/
网友答案:

in jQuery:

$("#temp").parents("tr").css('background-color', 'green');              
网友答案:

Try .parents()

$('#temp').parents('tr').first().css({'background-color': '#fff'});

.first() exclude elements in cause of nested tables.

Or .closest()

$('#temp').closest('tr').css({'background-color': '#fff'});
分享给朋友:
您可能感兴趣的文章:
随机阅读: