CSS selectors - how to select 'for' in CSS?

前端 未结 3 722
执笔经年
执笔经年 2020-12-10 14:26

I\'m using jQuery validation.

When for is now valid, validator is creating those:

相关标签:
3条回答
  • 2020-12-10 14:49

    Using the (CSS2) attribute selector:

    .error[for="username"]
    

    This will work in IE8+ and all modern browsers.


    IE7's attribute selector is buggy: as explained here, to match for you must use htmlFor.

    So, if you need IE7 support, use this:

    .error[for="username"], .error[htmlFor="username"]
    

    Thanks IE7, for making my answer over twice as long as it needed to be.

    0 讨论(0)
  • 2020-12-10 14:55

    In the css:

    .error[for=username] {
    }
    

    In the jQuery

    $('.error[for=username]')
    
    0 讨论(0)
  • 2020-12-10 15:12

    Any attribute can be selected with CSS or jQuery using the [] notation. CSS applies to any XML-like syntax, not just HTML -- it doesn't know (or care) what attributes are "valid" as long as the structure is well-formed.

    .error[for='username'] {
    
    }
    

    or for a "starts with"

    .error[for^='userprefix'] {
    
    }
    
    0 讨论(0)
提交回复
热议问题