【问题标题】:How to select elements that have no classes?如何选择没有类的元素?
【发布时间】:2015-03-13 22:47:21
【问题描述】:

我需要选择所有没有类说明符的 <input type="submit"> 元素。

与:

<input class="Submit" type="submit" value="Save" name="action_1">
<input class="Button" type="submit" value="Save as" name="action_2">
<input                type="submit" value="Save and Continue" name="action_3">
<input class="Cancel" type="submit" value="Cancel" name="action_4">

它应该只选择第三个。

我可以想象这个 CSS:

input[type="submit"]:not(ANY CLASS){
}

但是,我应该写成“ANY CLASS”吗? 或者有什么不同的方法吗?我可以在那里列举所有已知的类,但这很乏味,并且可能会随着时间的推移而改变。

注意:

【问题讨论】:

  • 您不能简单地将更改应用到其他类并保留默认的.input 选择器以适合没有类的输入吗?
  • @Brewal 不幸的是,我正在创建一个应用在现有页面之上的“超级 CSS”。我的 CSS 应该适合与 Firefox 的“时尚”插件一起使用。
  • 我投票支持重新开放,因为链接的答案实际上并没有回答这个问题。虽然它们没有错,但它们只提供了一种解决方法,而不是答案。
  • 作为回答第二个重复问题的人,我可以确认它完全不同。

标签: html css css-selectors pseudo-class


【解决方案1】:

您可以使用:not([class]),这意味着选择一个没有class 属性的input 元素。

input[type="submit"]:not([class]){
  color: red;
}
<input class="Submit" type="submit" value="Save" name="action_1">
<input class="Button" type="submit" value="Save as" name="action_2">
<input                type="submit" value="Save and Continue" name="action_3">
<input class="Cancel" type="submit" value="Cancel" name="action_4">

【讨论】:

    【解决方案2】:

    你可以使用这样的选择器:

    input:not([class]) {
        /* style for inputs without classes */
    }
    


    JSFIDDLE DEMO

    reference

    【讨论】:

    • 感谢,特别是 JSFiddle 演示。不错!
    • 很高兴为您提供帮助。如果您网站上的类是动态的(通过 javascript 从元素中添加/删除),您可能还需要考虑捕获这种替代方法 input[class=""] { ... },它将捕获具有空白类的元素 -- updated JSfiddle
    猜你喜欢
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 2011-06-26
    • 2021-01-18
    • 2019-01-17
    • 2013-02-04
    相关资源
    最近更新 更多