我有一个表结构:
<table style="width: 100%;">
<tr>
<td>
<a href="#" class="xx">one</a>
</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr>
<td>
<a href="#" class="xx">Four</a>
</td>
<td>Five</td>
<td>Six</td>
</tr>
<tr>
<td>
<a href="#" class="xx">Seven</a>
</td>
<td>Eight</td>
<td>Nine</td>
</tr>
</table>
CSS:
.yy
{
background-color: red;
}
现在的问题是如果我点击 <a>
其对应的<tr>
元素( <tr>
持有 <a>
)背景应该是红色的,如果我再次点击相同的 <a>
它应该恢复正常。
如何识别<tr>
<a>
被点击。
编辑: 我试过: $(".yy").not($(this).parent().parent()).removeClass("yy"); $(this).parent().parent().toggleClass("yy"); 它奏效了。
最佳答案
$('a').live('click', function(){
$(this).parent().parent() //this is how you select the <tr> that the <a> is in
//first .parent() gets the <td> second .parent() gets the <tr>
});
如果您需要更多,请告诉我。可能有更好的方法,但我并不乐观。
关于javascript - 如何选择包含 <a> 的 <tr> 并更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3593598/