简体   繁体   English

使用后代选择器时,CSS 兄弟选择器会中断吗?

[英]Does CSS sibling selector break when using decendant selectors?

My last question was closed as being a "duplicate" of two totally unrelated questions and the instructions say to post a new question.我的最后一个问题被关闭为两个完全不相关的问题的“重复”,并且说明说要发布一个新问题。 Seems a bit messy, but ok;看起来有点乱,但还好; here's the question again (I changed the title for clarity):这里又是一个问题(为了清楚起见,我更改了标题):

=============================================== ================================================

I'm trying to target a link that ONLY occurs after an image, but they're in separate tags like so:我试图定位一个只出现在图像之后的链接,但它们位于单独的标签中,如下所示:

<p>
  <a href=whatever>
    <img stuff>
  </a>
</p>
<p>
  <a href=something>This should be a different color</a>

This "works":这个“作品”:

p + p>a {
  color: red;
}

But it's a bit inspecific.但这有点不具体。 I want to do something like this, but I think it's looking for the sibling of the IMAGE not the PARAGRAPH when I do this:我想做这样的事情,但我认为它在寻找 IMAGE 的兄弟姐妹而不是 PARAGRAPH 当我这样做时:

p>a>img + p>a {
  color:red
}

So the first example works, but might trigger on something I didn't intend.所以第一个例子有效,但可能会触发我不想要的东西。 The second doesn't work at all and maybe it can't.第二个根本不起作用,也许它不能。 I'd love to use classes or ids, but this is in a project using markdown which gets translated to HTML.我很想使用类或 id,但这是在一个使用 Markdown 的项目中,它被翻译成 HTML。 The only way I have to target elements is by general structure and not attributes (unless you know another way?)我必须定位元素的唯一方法是通过一般结构而不是属性(除非您知道另一种方法?)

It might be helpful to know that browsers match selectors left to right , and sometimes reading it yourself that way can be helpful.了解浏览器从左到右匹配选择器可能会有所帮助,有时自己以这种方式阅读可能会有所帮助。 Also, keep in mind that your spacing is implying groupings that CSS does not care about.另外,请记住,您的间距意味着 CSS 不关心的分组。

So if we read it right to left, we find that this p + p>a would be read as "an anchor inside of a paragraph and that paragraph is itself next to another paragraph".因此,如果我们从右到左阅读它,我们会发现这个p + p>a将被理解为“一个段落内的锚点,而该段落本身紧挨着另一个段落”。

On the other hand, p>a>img + p>a would be read as "an anchor inside of a paragraph, which is itself next to an image that is inside of an anchor inside of a paragraph."另一方面, p>a>img + p>a将被解读为“段落内的锚点,它本身就在段落内锚点内的图像旁边”。 By leaving out spaces between your selectors and > you are implying a grouping that does not exist.通过在选择器和>之间留出空格,您暗示了一个不存在的分组。 So this would actually match like this:所以这实际上会像这样匹配:

<p>
  <a>
    <img/>
    <p>
      <a>THIS HERE IS WHAT IS TARGETED</a>
    </p>
  </a>
</p>

Obviously this structure is absurd, but you understand I hope the way the targeting is working.显然这种结构是荒谬的,但你明白我希望定位的工作方式。 There is no way to traverse back up to a parent (there is no < selector), nor can you group selectors like (p>a>img) + (p>a) , so unfortunately the selection approach you want here is not feasible/possible.有没有办法来遍历备份到父(有没有<选择),也无法将组选择像(p>a>img) + (p>a)所以很遗憾你想在这里选择的做法是行不通的/可能的。

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

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