简体   繁体   English

选择奇怪的嵌套元素

[英]Selecting strangely nested elements

I can't seem to find a situation similar enough that I can figure out a solution. 我似乎找不到足够类似的情况来找出解决方案。 Without changing the html and adding classes and ids how can I select the a tag and the pre tag from the following code? 在不改变HTML和添加class和id我怎么能选择a标签和pre从下面的代码标签? I've included my attempts. 我已经包括了我的尝试。

 div.info p.c6 span a { background-color: red; } p.c6 span pre { background-color: blue; } 
 <div class="info"> <h1 class="c4"> <a name="h.6q469n2havqi"></a><span>Title</span> </h1> <p class="c6"> <span> <pre> words </pre> <br> <a href="#h.c2v2rn37qt4d">Top of Page</a> <br> </span> </p> </div> 

Because the p element can only contain phrasing content , and in this case it contains flow content , the browser is closing the element and invalidating your selectors. 由于p元素只能包含短语内容 ,在这种情况下,它包含流内容 ,因此浏览器正在关闭该元素并使选择器无效。

In short, the pre element is flow content and cannot be contained inside a p element. 简而言之, pre元素是流内容 ,不能包含在p元素内。 Therefore, the browser is overriding your HTML structure to maintain valid mark-up. 因此,浏览器将覆盖您的HTML结构以维护有效的标记。 Here's what it looks like: 看起来是这样的:

在此处输入图片说明

The browser has essentially converted your p descendants into p siblings. 浏览器实质上已将您的p后代转换为p同级。

You need to restructure your HTML for your selectors to work. 您需要重组HTML才能使选择器正常工作。

If you can't change the HTML, a sibling selector will work. 如果您无法更改HTML,则同级选择器将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM