CSS selector with period in ID

前端 未结 3 1882
南方客
南方客 2020-11-28 06:37

The HTML spec allows for periods (.) in an id:

However, using a CSS ID selector rule will not match correc

相关标签:
3条回答
  • 2020-11-28 07:03

    You could also use the attribute selector like this:

    [id='some.id'] {
      color: #f00;
    }
    
    0 讨论(0)
  • 2020-11-28 07:07

    After digging through the specs some more, I found the CSS spec does allow for backslash (\) escaping like most languages.

    So in my example, the following rule would match:

    #some\.id {
      color: #f00;
    }
    
    0 讨论(0)
  • 2020-11-28 07:10

    Since you are using id, you can also use document.getElementById() which checks the id value as a normal string and not a CSS selector. e.g. the following works too:

    const myElem = document.getElementById("some.id");
    

    The only drawback is, you can't limit the scope of search, like you could with querySelector e.g. someElement.querySelector(). but since Ids should always be unique, a document wide search with id is valid.

    0 讨论(0)
提交回复
热议问题