我的浏览器与我的CSS不匹配(但jQuery确实如此)

时间:2016-01-06 16:02:03

标签: jquery css jquery-selectors css-selectors

考虑这些课程:

log
.bio-target-measure {
  opacity: 1;
}
.bio-target-measure:not(.lowerThird-25.lowerThird-active):not(.lowerThird-50.lowerThird-active):not(.lowerThird-100.lowerThird-active):not(.middleThird-25.middleThird-active):not(.middleThird-50.middleThird-active):not(.middleThird-100.middleThird-active):not(.upperThird-25.upperThird-active):not(.upperThird-50.upperThird-active):not(.upperThird-100.upperThird-active):not(.bud-25.bud-active):not(.bud-50.bud-active):not(.bud-100.bud-active) {
  opacity: 0 !important;
}

这些jQuery调用:

<div class="mark bio-target-measure upperThird-100" title="Muestra
        Plaga: Oidio
        ID: [4]
        Tercios: [&quot;upperThird&quot;]" style="width:100.0%;height:100%;color:white;background-color:#6a6200;">Test</div>

但是这个元素的不透明度,满足var measures = $('.bio-target-measure'); measures.length; // evaluates to 1. measures.is('.bio-target-measure:not(.lowerThird-25.lowerThird-active):not(.lowerThird-50.lowerThird-active):not(.lowerThird-100.lowerThird-active):not(.middleThird-25.middleThird-active):not(.middleThird-50.middleThird-active):not(.middleThird-100.middleThird-active):not(.upperThird-25.upperThird-active):not(.upperThird-50.upperThird-active):not(.upperThird-100.upperThird-active):not(.bud-25.bud-active):not(.bud-50.bud-active):not(.bud-100.bud-active)'); // evaluates to true. var emptyMeasures = $('.bio-target-measure:not(.lowerThird-25.lowerThird-active):not(.lowerThird-50.lowerThird-active):not(.lowerThird-100.lowerThird-active):not(.middleThird-25.middleThird-active):not(.middleThird-50.middleThird-active):not(.middleThird-100.middleThird-active):not(.upperThird-25.upperThird-active):not(.upperThird-50.upperThird-active):not(.upperThird-100.upperThird-active):not(.bud-25.bud-active):not(.bud-50.bud-active):not(.bud-100.bud-active)'); emptyMeasures.length; // evaluates to 1. 的选择器(正如使用jQuery看到的),是1.我的谷歌浏览器(47.0.2526.106,Ubuntu 15.04, 64位)浏览器仅使用 第一条规则(是澄清:不存在opacity 0的规则 - 但是覆盖:匹配中完全没有。)< / p>

问题:是不是规则(因为我用它)一个有效的CSS选择器?我知道它适用于jQuery,但我问的是CSS。

2 个答案:

答案 0 :(得分:4)

根据@Harry的评论,你不能在:not()中使用组合选择器。 (由@TJCrowder提供的证据::notsimple selectorsequence of selectors

在这种情况下,您需要重新修改逻辑:

.bio-target-measure.lowerThird-25:not(.lowerThird-active),
.bio-target-measure.lowerThird-50:not(.lowerThird-active),
...

使用LESS或类似的更高级别的CSS可以轻松实现这一点:

.bio-target-measure {
    &.lowerThird-25, &.lowerThird-50, &.lowerThird-100 {
        &:not(.lowerThird-active) {
            opacity: 0 !important;
        }
    }
    /* define similar blocks for middleThird and upperThird */
}

上面将编译成第一个代码块中建议的复杂选择器,同时更容易阅读。

答案 1 :(得分:0)

事实上,@ NietTheDarkAbsol回答的是-half-预期答案:我的选择器在CSS中无效(是的,在jQuery中)。

但是,重写逻辑还需要保留类关系。这意味着:

  

同时不满足以下4个条件的.bio-target-measure必须具有0的不透明度:

  • .lowerThird-(25 | 50 | 100).lowerThird活性
  • .middleThird-(25 | 50 | 100).middleThird活性
  • .upperThird-(25 | 50 | 100).upperThird活性
  • .bud-(25 | 50 | 100)的.bud活性

未设置相应-active过滤器的至少一个时,他的答案中的选择器产生0。由于每个标记可以使用多个-active过滤器,因此必须以默认为负的方式重写解决方案(即默认情况下不透明度为0,具有其他类时不为0)。 sass脚本是这样的:

.bio-target-measure {
  @each $lowerThird in 0, 25, 50, 100 {
    @each $middleThird in 0, 25, 50, 100 {
      @each $upperThird in 0, 25, 50, 100 {
        @each $bud in 0, 25, 50, 100 {
          $lowerThirdClass: '.lowerThird-#{$lowerThird}.lowerThird-active';
          $middleThirdClass: '.middleThird-#{$middleThird}.middleThird-active';
          $upperThirdClass: '.upperThird-#{$upperThird}.upperThird-active';
          $budClass: '.bud-#{$bud}.bud-active';

          @if $lowerThird == 0 { $lowerThirdClass: ''; }
          @if $middleThird == 0 { $middleThirdClass: ''; }
          @if $upperThird == 0 { $upperThirdClass: ''; }
          @if $bud == 0 { $budClass: ''; }

          $sum: $lowerThird + $middleThird + $upperThird + $bud;
          @if $sum > 50 {
            $sum: 100;
          }

          &#{$lowerThirdClass}#{$middleThirdClass}#{$upperThirdClass}#{$budClass} {
            opacity: $sum / 100;
          }
        }
      }
    }
  }
}