Why is “#.id” a bad selector in CSS/jQuery yet it works in an HTML anchor?

前端 未结 5 849
迷失自我
迷失自我 2021-01-07 17:16

I\'m using JSDoc. It generates ids with a period as in


If another part of the page has



        
5条回答
  •  再見小時候
    2021-01-07 17:39

    You have to escape . with \\ before querying for elements. Replace

    document.querySelector('#.someMethodName');
    

    To

    document.querySelector('#\\.someMethodName');
    

    Also note that technically, for HTML 4 required ID value format is specified below:

    ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

    So .[A-Za-z] is invalid one..

提交回复
热议问题