deeporist
V2EX  ›  CSS

CSS 选择器写法请教(反正我是万策尽矣了)

  •  
  •   deeporist · Oct 18, 2017 · 3607 views
    This topic created in 3130 days ago, the information mentioned may be changed or developed.

    A: div class="R clearfix " data-field="xxxx"

    B: div class="R clearfix v9d1365e44 "

    C: div class="R clearfix " data-field="xxxx"

    提取 B, "v9d1365e44"是一串随机字符, 由字母和数字构成.

    首先 B 出现在随机位置没办法用第几子元素定位, 以整个 class 字串来定位的话只要随机字串一变就失效.

    用:not[class$="clearfix"]也不行, 因为在有的页面 A 和 C 也不是以 clearfix 结尾的(cbright 还是啥的).

    我还能想到的就是 regex 去匹配那串随机字符, 但是怎么写进 css 选择器里我就不知道了, 怎么查都没查到, 更不用说 regex 我都是睡一觉就全忘得一干二净......

    6 replies    2017-10-18 13:24:20 +08:00
    Carseason
        1
    Carseason  
       Oct 18, 2017 via iPhone
    用^匹配开始,或者正则
    Troevil
        2
    Troevil  
       Oct 18, 2017
    难道不是 :not([data-field])
    cowpea
        3
    cowpea  
       Oct 18, 2017
    是你自己页面的话直接给 B 加上别的 css 或者 id 不就可以了。
    你控制不了的页面可以先获取到所有 css 再去判断长度
    https://gist.github.com/onionc/2990e5cb3dd874037199d7b982c19d45
    lianyue
        4
    lianyue  
       Oct 18, 2017
    [class^="R clearfix "]

    这样
    cowpea
        5
    cowpea  
       Oct 18, 2017
    上面打错,加上 class 不是 css
    Chingim
        6
    Chingim  
       Oct 18, 2017 via Android
    .R.clearfix:not([data-field]) {
    color:red;
    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5560 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 07:34 · PVG 15:34 · LAX 00:34 · JFK 03:34
    ♥ Do have faith in what you're doing.