0

世界でこれが機能しない理由を誰が知っていますか:

$( "p  p" ).each(function() {
    $(this).css("color", "green");
});

「p > p」、またはネストされた段落要素を持つものと同様に。divやその他の要素で問題ありませんが

たとえば、これを確認してください:

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

<script>
  alert('length='+$('p p').length); // 0
</script>

<div style="border:1px solid;">
  <p>The first paragraph in  div.</p>
  <p>The second paragraph in div (and the 2nd child in this div).</p>
  <p>
    <span>Span within p (WILL BE fount as "p > span")</span>
    <p>P within p (1).</p> 
    <span>Span within p (will NOT be fount as "p > span")</span>
    <p>P within p (2).</p>
  </p>
</div>
<p>
    <span>Span within p (WILL BE fount as "p > span")</span>
    <p>P within p (1).</p> 
    <span>Span within p (will NOT be fount as "p > span")</span>

4

2 に答える 2

0

<p>デバッグ ツールでレンダリングされた html を確認すると、ネストしていた要素がネストされていないことがわかります。それらは別の要素としてレンダリングされます。したがって、html がレンダリングされ、または を使用して DOM を操作しようと$('p p')する$('p>p')と、上記の式または DOM 操作の構文で指定された要素は存在しません。

<div style="border:1px solid;">
  <p>The first paragraph in  div.</p>
  <p>The second paragraph in div (and the 2nd child in this div).</p>
  <p>
    <span>Span within p (WILL BE fount as "p &gt; span")</span>
    </p><p>P within p (1).</p> 
    <span>Span within p (will NOT be fount as "p &gt; span")</span>
    <p>P within p (2).</p>
  <p></p>
</div>
<p>
    <span>Span within p (WILL BE fount as "p &gt; span")</span>
    </p><p>P within p (1).</p> 
    <span>Span within p (will NOT be fount as "p &gt; span")</span>

@Ish と @j08691 が述べたように、<p>タグはネストできません。ブラウザーのデバッグ ツールを使用すると、ブラウザーで明確に確認できます。詳細については、別のSOの回答を確認してください

于 2016-08-30T20:59:53.850 に答える