0

子セレクターを複製せずに、選択した親の特定の子のみをターゲットにして、セレクターを長さの点でチャートから外す方法はありますか?

たとえば、特定の div の最初の子段落を選択したいとします。

CSSは次のようになります

.funny > p:first-child, .sad > p:first-child {
 color: red
}

マークアップ

<div class="funny">
  <p>This is red</p>
  <p>This is whatever</p>
  <p>This is whatever</p>
  <p>This is whatever</p>
</div>

<div class="wildcrazy">
  <p>This is whatever</p>
  <p>This is whatever</p>
  <p>This is whatever</p>
  <p>This is whatever</p>
</div>

<div class="sad">
  <p>This is red</p>
  <p>This is whatever</p>
  <p>This is whatever</p>
  <p>This is whatever</p>
</div>

明らかに、私がターゲットにしたい面白いものや悲しいもの以外の多くの div には、他の多くの最初の段落が存在する可能性があります。

私の質問は次のとおりです。どのように子を宣言し、すべての親を先頭に追加できますか? 何かのようなもの:

.funny, .sad, .another_div_class0, .another_div_class1, .another_div_class2, .another_div_class3 > p:first-child

それ以外の

.funny p:first, .sad p:first-child, .another_div_class0 p:first-child, .another_div_class1 p:first-child, .another_div_class2 p:first-child, .another_div_class3 p:first-child

4

4 に答える 4

3

jqueryでこれ:

.funny, .sad, .another_div_class0, .another_div_class1, .another_div_class2, .another_div_class3 > p:first

これに翻訳されます:

$('.funny, .sad, .another_div_class0, .another_div_class1, .another_div_class2, .another_div_class3').children('p:first')
于 2012-08-02T18:42:40.057 に答える
0

CSSではわかりませんが、jqueryでできます。すべての親クラスをセレクターに入れ、セレクターの子である場合にのみ ap が必要であると述べます。

$('.funny, .sad, .another_div_class0, .another_div_class1, .another_div_class2, .another_div_class3').each( function() {
    $('p:first-child', this).css(Do whatever you want);
});
于 2012-08-02T18:42:34.883 に答える
0

divこのように選択したいのに別のクラスを追加してみませんか

<div class="funny selectthis">
  <p>This is red</p>
  <p>This is whatever</p>
  <p>This is whatever</p>
  <p>This is whatever</p>
</div>

<div class="wildcrazy">
  <p>This is whatever</p>
  <p>This is whatever</p>
  <p>This is whatever</p>
  <p>This is whatever</p>
</div>

<div class="sad selectthis">
  <p>This is red</p>
  <p>This is whatever</p>
  <p>This is whatever</p>
  <p>This is whatever</p>
</div>

次に、これを使用します

$('.selectthis').find('p:first').css('color','red');​

http://jsfiddle.net/U6qhb/

于 2012-08-02T18:59:20.673 に答える