jQuery: dealing with a space in the id attribute

前端 未结 5 2148
失恋的感觉
失恋的感觉 2020-12-01 16:29

I have an element with id=\"A B\". The following code fails:




        
5条回答
  •  借酒劲吻你
    2020-12-01 16:50

    While it’s technically invalid to have a space in an ID attribute value in HTML (see @karim79’s answer), you can actually select it using jQuery.

    See http://mothereffingcssescapes.com/#A%20B:

    
    

    jQuery uses a Selectors API-like syntax, so you could use $('#A\\ B'); to select the element with id="A B".

    To target the element in CSS, you could escape it like this:

    
    

提交回复
热议问题