1

要素の親とその兄弟にクラスを適用しようとしています。これまでのところ、2 行の jQuery を適用することによってのみ成功しています。これにより、最終結果が 2 段階で適用され、見栄えが悪くなります。

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

これら 2 行のコードを組み合わせて、クラスをすべての要素 (親とその兄弟) に同時に適用する方法はありますか?

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 に答える