javascript 选择所有类型不是复选框的输入标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9956545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:21:09  来源:igfitidea点击:

Select all input tags where type is not checkbox

javascriptcssinputcss-selectors

提问by daGrevis

I need CSS selector that match all input tags where type is notcheckbox.

我需要匹配所有输入标签的 CSS 选择器,其中 type不是checkbox

This match:

这场比赛:

<input value="Meow!" />

<input value="Meow!" />

<input type="password" />

<input type="password" />

...but this does not:

...但这不是

<input type="checkbox" />

<input type="checkbox" />

Because type is checkbox!

因为类型是checkbox

This is what I have at the moment:

这就是我目前所拥有的:

input:not(type="checkbox")

input:not(type="checkbox")

Unfortunately, it does notwork!

不幸的是,它并没有工作!

So here comes my questions:

所以我的问题来了:

  1. How to fix my CSS3 selector?
  2. Is it possible without CSS3 and JavaScript?
  3. Is it possible without CSS3, but with use of JavaScript?
  1. 如何修复我的 CSS3 选择器?
  2. 没有 CSS3 和 JavaScript 有可能吗?
  3. 没有 CSS3,但使用 JavaScript 是否可能?

Thanks in any advice!

感谢任何建议!

回答by BoltClock

  1. Your attribute selector is missing the square brackets:

    input:not([type="checkbox"])
    
  2. If you're applying styles you will need to make use of an override rule in CSS:

    input {
        /* Styles for all inputs */
    }
    
    input[type="checkbox"] {
        /* Override and revert above styles for checkbox inputs */
    }
    

    As you can imagine, it's almost impossible to do this for form elements because their browser-default styles aren't well-defined in CSS.

  3. jQuery provides a :checkboxselectorthat you can use:

    $('input:not(:checkbox)')
    

    You can also use the same selector as you do in CSS:

    $('input:not([type="checkbox"])')
    
  1. 您的属性选择器缺少方括号:

    input:not([type="checkbox"])
    
  2. 如果要应用样式,则需要使用 CSS 中的覆盖规则:

    input {
        /* Styles for all inputs */
    }
    
    input[type="checkbox"] {
        /* Override and revert above styles for checkbox inputs */
    }
    

    可以想象,对表单元素执行此操作几乎是不可能的,因为它们的浏览器默认样式在 CSS 中没有明确定义。

  3. jQuery 提供了一个可以使用的:checkbox选择器

    $('input:not(:checkbox)')
    

    您还可以使用与在 CSS 中相同的选择器:

    $('input:not([type="checkbox"])')
    

回答by Alexander Pavlov

input:not([type="checkbox"])

input:not([type="checkbox"])