我想将一个类应用到元素的父母和它的兄弟姐妹。到目前为止,我只通过应用两行jQuery,使得最终结果在两个阶段中的应用非常难看,才取得了成功。jQuery addClass父和它的兄弟姐妹
<style type="text/css">
td.rostered {
opacity: 0.3;
}
</style>
<table>
<tr class="flight_search">
<td class="" style="width: 6%; text-align: left"><img id="525" class="roster" src="images/link_image.png" alt="Link Image"></td>
<td class="" style="width: 7%">Option 1</td>
<td class="" style="width: 24%">London -<strong>Paris</strong></td>
<td class="" style="width: 25%">Dep: <strong>0355</strong> Arr: 0615 (02:20)</td>
<td class="" style="width: 30%">Boeing 767</td>
<td class="" style="width: 8%"><a class="rego" href="#">ABCDE</a></td>
</tr>
</table>
当用户点击图像(ID =“525”)的一些AJAX运行来指派飞行到用户的名册。然后它返回id并运行一些jQuery来灰化该表的那一行,表明它已经被列出。
function set_rostered(id) {
//This line adds the class 'rostered' the siblins of the <td>
$("#" + id).parent().siblings().addClass('rostered');
//This line removes the id so the flight cannot be rostered again and adds the class to the parent <td> itself.
$("#" + id).attr('id', '').parent().addClass('rostered');
}
有没有一种方法,我可以结合这两行代码,使类适用于所有元素(父和它的兄弟姐妹)在同一时间?
那么,你可以重构你的CSS来定位'tr.rostered td'而不是'td.rostered'。这会消除你的第一条线。 – FrankieTheKneeMan 2013-04-20 00:08:53
使用jQuery版本 – 2013-04-20 00:11:03
jQuery版本是1.9.1。 – Barbs 2013-04-20 00:38:54