CSS selector for text input fields?

后端 未结 9 784
广开言路
广开言路 2020-12-07 07:46

How can I target input fields of type \'text\' using CSS selectors?

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 08:15

    input[type=text]
    

    or, to restrict to text inputs inside forms

    form input[type=text]
    

    or, to restrict further to a certain form, assuming it has id myForm

    #myForm input[type=text]
    

    Notice: This is not supported by IE6, so if you want to develop for IE6 either use IE7.js (as Yi Jiang suggested) or start adding classes to all your text inputs.

    Reference: http://www.w3.org/TR/CSS2/selector.html#attribute-selectors


    Because it is specified that default attribute values may not always be selectable with attribute selectors, one could try to cover other cases of markup for which text inputs are rendered:

    input:not([type]), // type attribute not present in markup
    input[type=""], // type attribute present, but empty
    input[type=text] // type is explicitly defined as 'text'
    

    Still this leaves the case when the type is defined, but has an invalid value and that still falls back to type="text". To cover that we could use select all inputs that are not one of the other known types

    input:not([type=button]):not([type=password]):not([type=submit])...
    

    But this selector would be quite ridiculous and also the list of possible types is growing with new features being added to HTML.

    Notice: the :not pseudo-class is only supported starting with IE9.

提交回复
热议问题