positive lookahead in css

后端 未结 2 1848
悲&欢浪女
悲&欢浪女 2021-02-14 15:55

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 so

相关标签:
2条回答
  • 2021-02-14 16:02

    Not very helpful right now but,

    In the future we could use the CSS4 selector :has() in future will help with this.

    There is no browser compatibility at all at this time.

    div:has(+div.specialClass)
    
    0 讨论(0)
  • 2021-02-14 16:26

    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;
    }
    

    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;
    }
    

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

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

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

    Demo: http://jsfiddle.net/jonathansampson/HLfCr/
    Source: http://dev.w3.org/csswg/selectors4/#subject

    0 讨论(0)
提交回复
热议问题