如何选择包含特殊字符的id ?

时间:2020-12-27 20:18:10

I'm working with an HTML piece that I cannot modify. One of the ids in the document is:

我正在处理一个无法修改的HTML片段。文件中的id之一是:

<div id="a>span.tex"> ... </div>

This is perfectly valid HTML5 syntax for ids, however, it is not possible to select this id in CSS without having troubles with > and . characters.

这对于ids来说是完全有效的HTML5语法,但是,在CSS中选择这个id是不可能的,因为>和。字符。

How can I select this id in CSS?

如何在CSS中选择这个id ?

2 个解决方案

#1


3  

You can escape special characters with a backslash:

你可以用反斜杠来转义特殊的字符:

#a\>span\.tex {
  color: red;
}
<div id="a>span.tex"> Some funky ID </div>

You can even use backslashes within your ID as long as you escape them with another backslash:

你甚至可以在你的ID中使用反斜线,只要你用另一个反斜线转义:

#a\\span\\tex {
  color: red;
}
<div id="a\span\tex"> Some funky ID </div>

In fact, lots of crazy strings are valid IDs in HTML5

事实上,很多疯狂的字符串在HTML5中是有效的id

#\¯\\\_\(\ツ\)\_\/\¯ {
  color: red;
}
<div id="¯\_(ツ)_/¯"> ¯\_(ツ)_/¯ - but why would you? </div>

#2


0  

use this:

用这个:

div[id="a>span.tex"] {
    style here;
}

this will select div with id equal to your id

这将选择id等于您的id的div

#1


3  

You can escape special characters with a backslash:

你可以用反斜杠来转义特殊的字符:

#a\>span\.tex {
  color: red;
}
<div id="a>span.tex"> Some funky ID </div>

You can even use backslashes within your ID as long as you escape them with another backslash:

你甚至可以在你的ID中使用反斜线,只要你用另一个反斜线转义:

#a\\span\\tex {
  color: red;
}
<div id="a\span\tex"> Some funky ID </div>

In fact, lots of crazy strings are valid IDs in HTML5

事实上,很多疯狂的字符串在HTML5中是有效的id

#\¯\\\_\(\ツ\)\_\/\¯ {
  color: red;
}
<div id="¯\_(ツ)_/¯"> ¯\_(ツ)_/¯ - but why would you? </div>

#2


0  

use this:

用这个:

div[id="a>span.tex"] {
    style here;
}

this will select div with id equal to your id

这将选择id等于您的id的div