简体   繁体   English

将:: selection CSS应用于div

[英]applying ::selection css to a div

When you apply a class with a particular color for the ::selection pseudoclass to a div, it only seems to effect the spans inside that div but not the rest of the text why is that? 当您将:: selection伪类的具有特定颜色的类应用于div时,似乎只影响该div内的跨度,而不影响文本的其余部分,为什么呢? and is there a way to apply a custom selection color to a div. 并有一种方法可以将自定义选择颜色应用于div。 See the JSFiddle for an example of attaching a class with a ::selection color to both a div (which does not work as I would expect) and the body which does work as expected. 请参见JSFiddle,以获取将具有:: selection颜色的类附加到div(它不能按我预期的方式)和可以按预期工作的正文的示例。

http://jsfiddle.net/m9ZxB/3/ http://jsfiddle.net/m9ZxB/3/

relevant css: 相关的CSS:

.green ::selection{
  background:green;   
}

There's an old bug report on bugzilla which relates to your problem. 关于bugzilla的旧错误报告与您的问题有关。

As you can see in comment 26 a selection is actually an extra anonymous element, that's why text-decoration of the surrounding anchor won't change its color since the selector only applies for this "child": <span>The word <span::selection>select</span::selection> is selected</span> . 正如您在评论26中所看到的,选择实际上是一个额外的匿名元素,这就是为什么周围锚点的文本修饰不会更改其颜色的原因,因为选择器仅适用于此“子项”: <span>The word <span::selection>select</span::selection> is selected</span>

and please also note 还请注意

Though this pseudo-element was in drafts of CSS Selectors Level 3, it was removed during the Candidate Recommendation phase, as it appeared that its behavior was under-specified, especially with nested elements, and interoperability wasn't achieved (based on discussion in the W3C Style mailing list). 尽管此伪元素在CSS Selectors Level 3的草稿中,但在“候选推荐书”阶段已被删除,因为它的行为似乎没有得到充分说明,尤其是使用嵌套元素时,并且未实现互操作性(基于W3C样式邮件列表)。

Visit this MDN post for more information 访问此MDN帖子以获取更多信息

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

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