CSS selector with period in ID

前端 未结 3 1915
南方客
南方客 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: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.

提交回复
热议问题