正面前瞻在css [英] positive lookahead in css

查看:106
本文介绍了正面前瞻在css的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在Perl正则表达式的积极前景的概念,即 q(?= u)匹配aq后跟au, 。我正在寻找类似于css的东西:我想匹配一个 div ,然后是一个兄弟 div.specialClass

I know in Perl regex the notion of the positive lookahead ie q(?=u) matches a q that is followed by a u, without making the u part of the match. I'm looking for something similar in css: I want to match a div, followed by a sibling div.specialClass.

<div>..</div>  <!-- div to match -->
<div class="specialClass">..</div>

我玩过+,但匹配 div.specialClass ,而我想要前面的 div

I've played around with + but that matches with div.specialClass, whereas I want the preceding div.

推荐答案

p>您还不能声明选择器的哪个部分是主题。主题始终是CSS中选择器的最后一个元素,直到我们得到移动的权力,可能使用 $ 语法。

You cannot yet declare which part of the selector is the subject. The subject is always the last element of the selector in CSS, until we get the power to move that, likely using the $ or ! syntax.

// Always selects the .specialClass div which follows another div
div + div.specialClass {
    color: red;
}

以后,您可以将第一个div ,可能具有以下语法:

In the future, you'll be able to make the first div the subject, likely with the following syntax:

// Selects any `div` preceding any `div.specialClass`
$div + div.specialClass { // or maybe div! + div.specialClass
    color: red;
}

您的唯一解决方法 JavaScript。像jQuery这样的工具会使这个很简单:

Your only workaround in the interim is to use JavaScript. A tool like jQuery would make this very trivial:

$("div + div.specialClass").prev();

现在是所有 div div.specialClass 之前的元素。

Which is now a reference to all div elements immediately preceding any div.specialClass.

演示:http://jsfiddle.net/jonathansampson/HLfCr/

资料来源: http://dev.w3.org/csswg/selectors4/#subject

这篇关于正面前瞻在css的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆