jQuery:处理 id 属性中的空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6802765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: dealing with a space in the id attribute
提问by Randomblue
I have an element with id="A B". The following code fails:
我有一个 id="A B" 的元素。以下代码失败:
<input type="text" id="A B">
<script>$("#A B").click(function(){alert();});</script>
The following code does not fail:
以下代码不会失败:
<input type="text" id="AB">
<script>$("#AB").click(function(){alert();});</script>
回答by Mathias Bynens
While it's technically invalid to have a space in an ID attribute value in HTML (see @karim79's answer), you can actually select it using jQuery.
虽然在 HTML 中的 ID 属性值中包含空格在技术上是无效的(请参阅 @karim79 的答案),但您实际上可以使用 jQuery 选择它。
See http://mothereffingcssescapes.com/#A%20B:
见http://mothereffingcssescapes.com/#A%20B:
<script>
// document.getElementById or similar
document.getElementById('A B');
// document.querySelector or similar
$('#A\ B');
</script>
jQuery uses a Selectors API-like syntax, so you could use $('#A\\ B');
to select the element with id="A B"
.
jQuery使用一个选择器API的语法,所以你可以使用$('#A\\ B');
选择与元素id="A B"
。
To target the element in CSS, you could escape it like this:
要定位 CSS 中的元素,您可以像这样转义它:
<style>
#A\ B {
background: hotpink;
}
</style>
回答by karim79
Neither HTML4 nor HTML5 allows space characters in ID attribute values.
HTML4 和 HTML5 都不允许在 ID 属性值中使用空格字符。
The HTML 4.01 spec states that ID tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens (-), underscores (_), colons (:), and periods (.). For the class attribute, there is no such limitation. Classnames can contain any character, and they don't have to start with a letter to be valid.
HTML5 gets rid of the additional restrictions on the id attribute. The only requirements left — apart from being unique in the document — are that the value must contain at least one character (can't be empty), and that it can't contain any space characters.
HTML 4.01 规范规定 ID 标记必须以字母 ([A-Za-z]) 开头,后面可以跟任意数量的字母、数字 ([0-9])、连字符 (-)、下划线 (_) 、冒号 (:) 和句点 (.)。对于 class 属性,没有这样的限制。类名可以包含任何字符,并且它们不必以字母开头才有效。
HTML5 摆脱了对 id 属性的额外限制。剩下的唯一要求——除了在文档中是唯一的——是值必须至少包含一个字符(不能为空),并且不能包含任何空格字符。
Source:
来源:
回答by TMS
As they mentioned you shouldn't use spaces in id. But anyway, wouldn't this be working for you?
正如他们提到的,你不应该在 id 中使用空格。但无论如何,这不适合你吗?
$("[id='A B']").click(...)
EDIT: yes it works, tested it!!! Don't kill me, standard-lovers!! ;-P
编辑:是的,它有效,经过测试!!!不要杀我,标准爱好者!!;-P
回答by Enki
There should not be space in an id, but this seems to works :
id 中不应该有空格,但这似乎有效:
$("[id=A B]")
回答by Jasper
ID tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
That quote is pulled from another stackoverflow answer: jquery IDs with spaces
那句话是从另一个 stackoverflow 答案中提取的:jquery IDs with space