0

だから私はこれを試しました:

input.find(" tr").each(function(){ ... });

私もこれを試しました:

input.find(" > tr").each(function(){ ... });

これらはどちらも機能しません。最初のものは、ネストされたテーブルの下にある場合でも、TR を選択します。テーブルに tbody などがある場合、2 つ目は機能しません。何か助けはありますか?

入力は次のように定義されます。

var input = $(".mySelectionScope");

DOM は次のようになります。

    <div class='mySelectionScope'>
        <div> // Optional
            <table>
                <tbody>
                    <tr>  // Select this one
                        <table>
                            <tr>  // Don't select this one
                            ....
4

5 に答える 5

0

.not()を使用して、tr の下にある tr を除外できます。

input.find("tr").not("tr tr")
于 2013-05-21T18:40:45.897 に答える
0

あなたはそれをフィルタリングすることができます:

input.find("tr").filter(function(){return !$(this).closest('tr').length}).each(...);
于 2013-05-21T18:43:18.830 に答える
0

trs のリストで first() を呼び出すのはどうですか?

http://api.jquery.com/first/

于 2013-05-21T18:43:31.450 に答える
0

find() の代わりに jQuery children() を使用する

ドキュメントから:

.children() メソッドは .find() とは異なり、.children() は DOM ツリーを 1 レベル下に移動するだけですが、.find() は複数のレベルを下に移動して子孫要素 (孫など) も選択できます。

オプションの div もいくつかのトリッキーさを生み出します...

var input = $('.mySelectionScope').children('div').length == 0 ?
        $('.mySelectionScope') : 
        $('.mySelectionScope > div'),
    rows = input.children('table').children('tbody').length == 0 ?
        input.children('table').children('tr') :
        input.children('table').children('tbody').children('tr');

tbody が常にすべてのブラウザーに挿入される場合、2 番目の if ステートメントは必要なく、代わりに使用できます。

    rows = input.children('table').children('tbody').children('tr');

かなり醜いですが、セレクターだけでこれを行うことはできません。

于 2013-05-21T20:00:45.523 に答える