How to select html nodes by ID with jquery when the id contains a dot?

后端 未结 8 1209
生来不讨喜
生来不讨喜 2020-11-22 08:22

If my html looked like this:


    

        
8条回答
  •  情书的邮戳
    2020-11-22 08:36

    Short Answer: If you have an element with id="foo.bar", you can use the selector $("#foo\\.bar")

    Longer Answer: This is part of the jquery documentation - section selectors which you can find here: http://api.jquery.com/category/selectors/

    Your question is answered right at the beginning of the documentation:

    If you wish to use any of the meta-characters ( such as 
    !"#$%&'()*+,./:;?@[\]^`{|}~ ) 
    as a literal part of a name, you must escape the character with 
    two backslashes: \\. For example, if you have an element with id="foo.bar", 
    you can use the selector $("#foo\\.bar"). The W3C CSS specification contains 
    the complete set of rules regarding valid CSS selectors. Also useful is the 
    blog entry by Mathias Bynens on CSS character escape sequences for identifiers.
    

提交回复
热议问题