jQuery ID选择器中的jQuery点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9930577/
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 dot in ID selector?
提问by Niklas R
Possible Duplicate:
How to select html nodes by ID with jquery when the id contains a dot?
I have a website that contains elements similar to this:
我有一个包含与此类似的元素的网站:
<p id="root.SomeCoolThing">Some Cool Thing</p>
I can not select the paragraph with jQuery like $('#root.SomeCoolThing')
because jQuery thinks, SomeCoolThing
is the class of an element with id="root"
.
我不能像 jQuery 一样选择段落,$('#root.SomeCoolThing')
因为 jQuery 认为,SomeCoolThing
是带有id="root"
.
How can I select this element with jQuery? I would like to avoid a construction like this:
如何使用 jQuery 选择此元素?我想避免这样的结构:
$(document.getElementById('root.SomeCoolThing'))
回答by charlietfl
Use the escaping rules from the jQuery selectors APIas follows:
使用jQuery 选择器 API的转义规则如下:
$('#root\.SomeCoolThing')
From the docs:
从文档:
To use any of the meta-characters (such as
!"#$%&'()*+,./:;<=>?@[\]^`{|}~
) as a literal part of a name, it must be escaped with with two backslashes:\\
. For example, an element withid="foo.bar"
, can use the selector$("#foo\\.bar")
.
要将任何元字符(例如
!"#$%&'()*+,./:;<=>?@[\]^`{|}~
)用作名称的文字部分,必须使用两个反斜杠对其进行转义:\\
. 例如,带有 , 的元素id="foo.bar"
可以使用选择器$("#foo\\.bar")
。
回答by mikevoermans
Using attr works:
使用 attr 作品:
$('p[id="root.SomeCoolThing"]')
回答by gdoron is supporting Monica
You need to escape special chars:
您需要转义特殊字符:
$('#root\.SomeCoolThing')
If you wish to use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~) as a literal part of a name, you must escape the character with two backslashes: \\. For example, if you have an element with id="foo.bar",you can use the selector $("#foo\\.bar").
如果您希望使用任何元字符(例如!"#$%&'()*+,./:;<=>?@[]^`{|}~)作为名称,您必须使用两个反斜杠转义字符:\\。例如,如果您有一个id="foo.bar"的元素,则可以使用选择器$("#foo\\.bar")。
回答by Carlos Quijano
You can escape period using something like this
你可以使用这样的东西来逃避时期
$("#root\.SomeCoolThing")
How do I get jQuery to select elements with a . (period) in their ID?http://groups.google.com/group/jquery-en/browse_thread/thread/ba072168939b245a?pli=1
如何让 jQuery 选择带有 . (句号)在他们的身上?http://groups.google.com/group/jquery-en/browse_thread/thread/ba072168939b245a?pli=1
回答by BradBrening
Shooting from the hip here, but if you cannot change the ID of the element, try using this selector:
此处从臀部拍摄,但如果您无法更改元素的 ID,请尝试使用此选择器:
$("p[id=root.SomeCoolThing]")
回答by Emmanuel N
Use two backslashes before each special character
在每个特殊字符前使用两个反斜杠
$('#root\.SomeCoolThing')