【问题标题】:Apply a class AND an attribute selector应用一个类和一个属性选择器
【发布时间】:2014-02-26 21:57:23
【问题描述】:

我想为这个元素设置样式:

<input class="myslider" type="range" value="20">

这是有效的:

input[class*="myslider"] {
    -webkit-appearance: none;
    width: 100px;
    background-color: black;
    height: 2px;
}

但这不起作用:

.myslider input[type=range] {
    -webkit-appearance: none;
    width: 100px;
    background-color: black;
    height: 2px;
}

为什么我不能应用类选择器属性选择器?

【问题讨论】:

  • 因为您在 .myslider 中选择了一个输入
  • .myslider input[type=range] - 这意味着myslider 是输入的父级。在这种情况下不是这样。
  • 谢谢。这是否意味着直接父母,或者它可能是任何祖先?
  • 输入 可以 嵌套在其他元素中,.myslider input {} 仍然可以工作 - 只要没有更具体的选择器覆盖它。

标签: css


【解决方案1】:

你要找的选择器是

input.myslider[type=range]

您当前的代码查找子元素,但未针对适当的元素。

【讨论】:

    【解决方案2】:

    因为您在 .mySlider 中选择输入。试试这个:

    .myslider[type='range'] {
        -webkit-appearance: none;
        width: 100px;
        background-color: black;
        height: 2px;
    }
    

    DEMO

    【讨论】:

      猜你喜欢
      • 2015-03-02
      • 1970-01-01
      • 2021-10-02
      • 2017-01-29
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      相关资源
      最近更新 更多