我应该如何转义 css/js 属性选择器 [attr=value]
中的属性?
具体来说,这是正确的吗?
document.querySelector('input[name="test[33]"]')
我正在寻找执行此操作的“标准方法”(如果有的话),因为我不希望 Sizzle 使用繁重的执行后备函数
最佳答案
是的,这是一种正确的方法。 Selectors Level 3 specification陈述如下:
Attribute values must be CSS identifiers or strings.
您问题中的示例使用字符串作为属性值。 “标识符”定义如下:
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...
因此,转义特殊字符并省略引号也是合法的:
document.querySelector('input[name=test\\[33\\]]')
关于javascript - 如何正确转义 css/js 属性选择器 [attr=value] 中的属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13987979/