2013-04-20 127 views
1

我想将一个类应用到元素的父母和它的兄弟姐妹。到目前为止,我只通过应用两行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'); 
} 

有没有一种方法,我可以结合这两行代码,使类适用于所有元素(父和它的兄弟姐妹)在同一时间?

+0

那么,你可以重构你的CSS来定位'tr.rostered td'而不是'td.rostered'。这会消除你的第一条线。 – FrankieTheKneeMan 2013-04-20 00:08:53

+0

使用jQuery版本 – 2013-04-20 00:11:03

+0

jQuery版本是1.9.1。 – Barbs 2013-04-20 00:38:54

回答

1

尝试增加回(与加回()函数)。

$("#" + id).parent().siblings().addBack().addClass('rostered'); 
+0

这与两行版本相同(我喜欢,现在是一行而不是两行)。但效果仍然适用于两个阶段,首先细胞中的文字突出显示,然后在大约半秒后显示。 – Barbs 2013-04-20 00:41:55

+0

@Barbs“Highlight”?也许你可以做一个小提琴来表明这两个舞台效果是什么,确切地说...... – FrankieTheKneeMan 2013-04-20 17:39:54

0

$( “#” + ID).attr( '身份证', '').parent()父() 。儿童()addClass( '名册');