本文介绍了定位< p& gt;段的后代元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行以下操作时,只有名人一词会变成红色。

When I do the following only the word, 'Celebrities', gets a red color.

body > p * {
  background-color: red;
}
<body>
  <p>I have met many <span>Celebrities</span> in my lifetime.
    <p>They</p> all were rich! I have a list
    <ul>
      <li>one</li>
      <li>two</li>
      <li>three</li>
    </ul>
  </p>
</body>

为什么,

根据我对后代选择器< span> 的理解,在第一个< p>内的< ul> 和< p> 应该是红色的,对吗?

From my understanding of descendant selectors the <span>, <ul> and <p> inside the first <p> should be red, right?

如果我按如下方式修改上面的内容,为什么呢?

If I modify the above as follows it works, why?

body > div * {
  background-color: red;
}
<div>I have met many <span>Celebrities</span> in my lifetime.
  <p>They</p> all were rich! I have a list
  <ul>
    <li>one</li>
    <li>two</li>
    <li>three</li>
  </ul>
</div>

推荐答案

p标签不能嵌套在其他p标签内。

p tags cannot be nested inside other p tags. When it parses your markup, it is inserting a (missing) closing p tag before the nested p tag.

<p>I have met many <span>Celebrities</span> in my lifetime. <p>
<p>They</p>

这篇关于定位&lt; p&amp; gt;段的后代元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 15:47