如何正确转义属性值在css / js属性选择器[attr =值]? [英] How to properly escape attribute values in css/js attribute selector [attr=value]?
问题描述
如何在css / js attibute选择器中转义属性 [attr = value]
?
How should I escape attributes in the css/js attibute selector [attr=value]
?
,这是正确的吗?
document.querySelector('input[name="test[33]"]')
我在寻找标准方式做这个,如果有的话,因为我不想Sizzle使用重执行后备功能
I'm looking for the "standard way" of doing this, if any, because I don't want Sizzle using a heavy-to-execute fallback function
推荐答案
是的,这是一种正确的方法。 选择器3级规范说明了以下内容:
Yes, that is one correct approach. The Selectors Level 3 specification states the following:
属性值必须是CSS标识符或字符串。
Attribute values must be CSS identifiers or strings.
您的问题使用字符串作为属性值。 标识符定义如下:
The example in your question uses a string as the attribute value. An "identifier" is defined as follows:
在CSS中,标识符...只能包含字符[a-zA-Z0- 9]和ISO 10646字符U + 00A0及更高,加连字符( - )和下划线(_);它们不能以数字,两个连字符或连字符后跟数字开头。标识符也可以包含转义字符和任何ISO 10646字符作为数字代码...
In CSS, identifiers... can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code...
转义特殊字符并省略引号:
So following that, it is also legal to escape the special characters and omit the quotes:
document.querySelector('input[name=test\\[33\\]]')
这篇关于如何正确转义属性值在css / js属性选择器[attr =值]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!