1

我正在尝试将一个类应用于元素的父级和兄弟级。到目前为止,我只通过应用两行 jQuery 取得了成功,这使得最终结果应用在两个阶段,看起来非常难看。

<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');
} 

有没有办法可以结合这两行代码使该类同时应用于所有元素(父元素和它的兄弟元素)?

4

2 回答 2

1

尝试将其添加回来(使用 addBack() 函数):

$("#" + id).parent().siblings().addBack().addClass('rostered');
于 2013-04-20T00:06:10.797 回答
0

$("#" + id).attr('id', '').parent().parent().children().addClass('rostered');

于 2013-04-20T00:56:36.400 回答