Home  >  Article  >  Web Front-end  >  关于子元素选择器的一个问题_html/css_WEB-ITnose

关于子元素选择器的一个问题_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-21 09:41:001325browse

本帖最后由 xyz121323693 于 2013-01-18 09:33:28 编辑

若是您欲望选择只作为 h1 元素子元素的 strong 元素,可以如许写:


 

h1 > strong {color:red;}
 

这个规矩会把第一个 h1 下面的 strong 元素变为红色,然则第二个 strong 不受影响:


 

This is very important.



This is really very important.





想问一下,怎么第二个就不受影响?

回复讨论(解决方案)


什么问题?只能猜测了 
这个是 h1 > strong {color:red;}
子代选择器,意识是  只有h1 的 直接子元素 strong  才有效果。
如果 strong里  还包含 strong的话 ,那么 对内层的 strong是不起作用的

如果 想 对h1 下的所有 strong 有效果,就用 后代选择器,h1 strong{color:red;}

什么问题?只能猜测了 
这个是 h1 > strong {color:red;}
子代选择器,意识是  只有h1 的 直接子元素 strong  才有效果。
如果 strong里  还包含 strong的话 ,那么 对内层的 strong是不起作用的
子元素是对所有的子元素有效啊。这儿就不行了。为什么?

要区分  子选择器(>) 跟  后代选择器 (空格) 的区别.
你说的问题原因是:外面嵌套了一个 元素节点,子代选择器 是 选得直接后代,不能被其他元素节点包围的 ,不如就不叫子选择器了。

明白了。现在好了

儿子和孙子的区别

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn