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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:28:55  来源:igfitidea点击:

jQuery dot in ID selector?

jqueryjquery-selectors

提问by Niklas R

Possible Duplicate:
How to select html nodes by ID with jquery when the id contains a dot?

可能的重复:
当 id 包含一个点时,如何使用 jquery 通过 ID 选择 html 节点?

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, SomeCoolThingis 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 with id="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')

docs:

文档:

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 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')