1

次のようなhtmlがあるとしましょう:

<div class="outer">
    <div>
        // another div
    </div>
    <p>First paragraph in outer div</p>
    <div>
        // another div
    </div>
    <a href="#">some anchor</a>
    <p>Second paragraph in outer div</p>
    <div>
        <p>Third paragraph in outer div</p> 
    </div>
    <div class="inner">
        <p>First paragraph in inner div</p>
        <p>Second paragraph in inner div</p>
    </div>
</div>

外側の DIV にあり、内側の DIV にはないパラグラフを取得するにはどうすればよいですか?

ありがとう

4

2 に答える 2

2

$('.outer').children('p')また$('.outer > p')

子セレクター ( 「親 > 子」)および.children()


外部divの直接の子ではない段落がある場合は、使用できます

$('.outer p').not('.inner p')

見る.not()

于 2013-03-21T03:26:27.423 に答える
1

直接の子セレクターを使用できます。

.outer > p {
    ...
}

その名前が示すようにp、クラス属性が であるすべての要素の直接の子であるすべての要素を取得しますouter

jQueryを使用すると、$('.outer > p').

アップデート:

コメントであなたが求めていることを理解する唯一の方法は次のとおりです。

.outer > p, .outer div:not(.inner) p {
    ...
}

これが行うことは、上記と同じ要素に加えて、ないタグ内のタグ内<p>にあるタグを選択することです。これがあなたが望むものを達成するための最良の方法だとは思いませんが、少なくとも解決策です。<div>.outer.inner

于 2013-03-21T03:26:35.410 に答える