我能找到的最新相关信息是W3C Selectors Level 4 Editor's Draft,但据我所知,它不再提及父选择器。
我知道有一个关于此事的 Google 调查,但现在已经结束了。
父选择器发生了什么? 它是否会被引入或已被删除?
我知道有一个关于此事的 Google 调查,但现在已经结束了。
父选择器发生了什么? 它是否会被引入或已被删除?
:has()
伪类取代而告终。该伪类的文档记录在这里(有一个有趣的锚点名称#relational
,我想知道那是否会保留)。:has()
伪类可能会保留,但由于其本质,它能否在CSS中实现还有待观察。请参阅同一草案的此部分了解有关实施配置文件的信息。
:has()
更加灵活多变的原因是,通过主题选择器,从未在任何草案中明确说明一个复杂选择器是否可以有多个主题选择器(因为单个复杂选择器只能有一个主题),和/或者功能伪类如:matches()
是否接受主题选择器。但是因为伪类是一个简单选择器,它完全适合现有的选择器语法,并且您可以可靠地假定:has()
会被接受在任何伪类被接受的地方。
例如,这使得以下选择器在理论上成为可能:
/*
* Select any p
* that is a sibling of a ul
* that has more than one li child.
*/
ul:has(> li:nth-of-type(2)) ~ p, /* p follows ul */
p:has(~ ul:has(> li:nth-of-type(2))) /* p precedes ul */
如果使用主题选择器,只有在:matches()
接受主题选择器的情况下才可能实现,而规范从未直接说明这一点:
ul:matches(! > li:nth-of-type(2)) ~ p, /* p follows ul */
!p ~ ul:matches(! > li:nth-of-type(2)) /* p precedes ul */
在CSS中没有父选择器。但是由于一个小技巧,我可以在本地CSS文件中使用.childinternal :parent { background-color: yellow }而不必深入使用jquery或javascript。
这个技巧有点肮脏,因为它改变了父元素的样式(这不是CSS所做的),并且不是真正的伪类。 但是在您的CSS样式表中,您可以像使用它一样使用它。
有两种实现方式(均已显示): 第一种是伪类:parent,这只能在本地样式表上执行,因为某些浏览器“不允许错误的伪类”。
另一种是运行所有样式表以检查“GetParent类引用”的方法。
对我来说,它有效。我通常采用第一种,这是最快的。
解决方案:
$(function() {
//First possibility when using local css file (=faster)//
$.when($.get("your local filename.css")).done(function(response) {
var spl = response.split(":parent");
if (spl.length > 1) {
var clas = $.trim((spl[0].split("}")[spl[0].split("}").length - 1]).replace(String.fromCharCode(10), '').replace(String.fromCharCode(13), ''));
var cs = $.trim((spl[1].split("}")[0].split("{")[1]).replace(String.fromCharCode(10), '').replace(String.fromCharCode(13), ''));
var eClas = $(clas).parent().attr('style');
eClas = eClas == undefined ? '' : (eClas + '').toString;
$(clas).parent().attr('style', eClas + ';' + cs);
}
});
});
// second possibility looping all used css files
for (var s = document.styleSheets.length - 1; s >= 0; s--) {
var cssRules = document.styleSheets[s].cssRules || document.styleSheets[s].rules || []; // IE support
for (var c = 0; c < cssRules.length; c++) {
if (cssRules[c].selectorText) {
if (cssRules[c].selectorText.indexOf('.GetParent') > -1) {
var spl = cssRules[c].cssText.split(".GetParent");
if (spl.length > 1) {
var clas = $.trim((spl[0].split("}")[spl[0].split("}").length - 1]).replace(String.fromCharCode(10), '').replace(String.fromCharCode(13), ''));
var cs = $.trim((spl[1].split("}")[0].split("{")[1]).replace(String.fromCharCode(10), '').replace(String.fromCharCode(13), ''));
var eClas = $(clas).parent().attr('style');
eClas = eClas == undefined ? '' : (eClas + '').toString;
$(clas).parent().attr('style', eClas + ';' + cs);
}
}
}
}
}
.childinternal :parent {
background-color: yellow
}
.childexternal .GetParent {
Background-color: green
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
Not affected Main parent
<div class="childinternal">
<p>Not affected parent (no parent selector)</p>
</div>
<div class="childinternal:parent">
<p>local css file used (:parent selector)
<span style='color:grey;font-size:0.6em'>Only works on local files so not possible to show in this snippet
</span>
</p>
</div>
</div>
<div>
<div class="childexternal .GetParent">
<p>external css file used (.GetParent class selector)</p>
<div class="x"></div>
</div>
</div>