代码之家  ›  专栏  ›  技术社区  ›  Yarin

文本输入字段的CSS选择器?

  •  345
  • Yarin  · 技术社区  · 15 年前

    9 回复  |  直到 9 年前
        1
  •  663
  •   Alin P.    12 年前
    input[type=text]
    

    或者,限制表单中的文本输入

    form input[type=text]
    

    或者,进一步限制到某种形式,假设它有id myForm

    #myForm input[type=text]
    

    注意:IE6不支持这一点,所以如果您想为IE6开发,可以使用IE7.js(正如Yi Jiang建议的那样),或者开始向所有文本输入添加类。

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


    因为 it is specified 使用属性选择器,默认属性值可能并不总是可选择的,可以尝试覆盖呈现文本输入的标记的其他情况:

    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'
    

    当定义了类型,但是有一个无效的值,并且仍然返回到type=“text”时,仍然会出现这种情况。为了说明我们可以使用select所有不是其他已知类型的输入

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

    list of possible types 随着新功能被添加到HTML中,它正在不断增长。

    :not pseudo-class 仅支持从IE9开始。

        2
  •  37
  •   Yi Jiang G-Man    10 年前

    input[type="text"] {
        font-family: Arial, sans-serif;
    }
    

    IE7及以上版本支持此功能。你可以用 IE7.js 如果需要支持IE6,则添加对此的支持。

    见: http://reference.sitepoint.com/css/attributeselector 了解更多信息

        3
  •  14
  •   garrettwinder    15 年前

    我通常在主样式表中使用选择器,然后生成一个特定于ie6的.js(jquery)文件,该文件向所有输入类型添加一个类。例子:

    $(document).ready(function(){
      $("input[type='text']").addClass('text');
    )};
    

        4
  •  7
  •   Aamir Shahzad    11 年前

    你可以用 :text 用于选择所有文本类型输入的选择器

    Working Fiddle

    $(document).ready(function () {
        $(":text").css({    //or $("input:text")
            'background': 'green',
            'color':'#fff'
        });
    });
    

    :文本 [type="text"] 相反。这对 IE6+

    $("[type=text]").css({  // or $("input[type=text]")
        'background': 'green',
        'color':'#fff'
    });
    

    [type=text] // or input[type=text] 
    {
        background: green;
    }
    
        5
  •  2
  •   Amit Mhaske    9 年前

    我在表格行字段中有输入类型文本字段。我用代码瞄准它

    .admin_table input[type=text]:focus
    {
        background-color: #FEE5AC;
    }
    
        6
  •  0
  •   Volker E. Lee    10 年前

    正如@Amir在上面所说,现在跨浏览器离开IE6的最好方法是使用

    [type=text] {}
    

    why is that important ?) 到目前为止, [type=text] features input[type=text] .

    从性能上看,已经没有任何负面影响了。

    规范化刚刚发布的v4.0.0 with lowered selector specificity

        7
  •  0
  •   Santosh Khalse    9 年前

    使用属性选择器,我们将CSS中的输入类型文本作为目标

    input[type=text] {
    background:gold;
    font-size:15px;
     }
    
        8
  •  0
  •   Navneet Kumar    7 年前

    输入[类型=文本]

    这将选择网页中的所有输入类型文本。

        9
  •  -1
  •   Nesha Zoric    8 年前

    [标题] 所有具有title属性的元素都被选中。

    [标题=香蕉] 具有title属性的“banana”值的所有元素。

    [标题~=香蕉] title属性值中包含“banana”的所有元素。

    [标题|=香蕉]

    [标题^=香蕉]

    [标题$=香蕉] title属性值以“banana”结尾的所有元素。

    title属性值包含子字符串“banana”的所有元素。

    参考: https://kolosek.com/css-selectors/