javascript javascript中的转义点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19596610/
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
escape dot in javascript
提问by Pars
jQuery selector can't select an ID if it contains a .
in it.
in my application ID name generates dynamically from usernames.
jQuery 选择器无法选择包含 a 的 ID .
。
在我的应用程序 ID 名称中,从用户名动态生成。
How can I escape any special character from tag IDsso that jQuery selector works well?
如何从标签 ID 中转义任何特殊字符,以便 jQuery 选择器正常工作?
for example, ID1: This.Is.ID.1
ID2: This.is.Another.ID
例如,ID1:This.Is.ID.1
ID2:This.is.Another.ID
Can anyone help me.
谁能帮我。
Thanks in advance.
提前致谢。
回答by Jesper Lindstr?m
If I understand you correctly, you want to modify a string that contains periods to have \\ in front of every period, to be supported as an id in the jQuery selector. Here is how to do that:
如果我理解正确,您想修改一个包含句点的字符串,以在每个句点前面加上 \\,以作为 jQuery 选择器中的 id 支持。以下是如何做到这一点:
var username = 'some.username.with.dots';
// Replace all periods with \. to
username = username.replace(/\./g, '\\.');
// find element that matches #some\.username\.with\.dots
$('#' + username).doSomethingWithjQuery();
.
means "any character" in regex, so you need to escape it by putting\
in front.- The
g
regex modifier means greedy, without it the replace expression would only replace the first.
with\\.
.
在正则表达式中表示“任何字符”,因此您需要通过放在\
前面来转义它。- 该
g
正则表达式改性装置贪心,没有它的替换表达式将仅替换所述第一.
与\\.
EditI tried my code, and it seems like you need to change the replace value to \\\\.
to make it become \\.
编辑我想我的代码,好像你需要更改替换值\\\\.
,使之成为\\.
回答by acdcjunior
The official jQuery FAQ has that exact question: How do I select an element by an ID that has characters used in CSS notation?They even have a function to help you escape. Quoting it:
官方 jQuery 常见问题解答有一个确切的问题:如何通过具有 CSS 表示法中使用的字符的 ID 选择元素?他们甚至有帮助你逃脱的功能。引用它:
... The colon ("
:
") and period (".
") are problematic within the context of a jQuery selector because they indicate a pseudo-class and class, respectively.In order to tell jQuery to treat these characters literally rather than as CSS notation, they must be "escaped" by placing two backslashes in front of them.
... 冒号 ("
:
") 和句点 (".
") 在 jQuery 选择器的上下文中是有问题的,因为它们分别表示伪类和类。为了告诉 jQuery 按字面意思处理这些字符而不是 CSS 符号,它们必须通过在它们前面放置两个反斜杠来“转义”。
// Does not work:
$( "#some:id" )
// Works!
$( "#some\:id" )
// Does not work:
$( "#some.id" )
// Works!
$( "#some\.id" )
The following function takes care of escaping these characters and places a "#" at the beginning of the ID string:
以下函数负责转义这些字符并在 ID 字符串的开头放置一个“#”:
function jq( myid ) {
return "#" + myid.replace( /(:|\.|\[|\]|,|=)/g, "\" );
}
The function can be used like so:
该函数可以像这样使用:
$( jq( "some.id" ) )
回答by Darknight
回答by STW
The the jQuery documentationstates that two backslashes are used to escape them.
在jQuery的文档指出两个反斜杠被用来逃避它们。
So your example would use $("#This\\.Is\\.ID\\.1")
所以你的例子会使用 $("#This\\.Is\\.ID\\.1")
回答by jmar777
You don't actually need to remote the .'s from the id. You simply need to escape any special characters in the selector. For example, to select the following element:
您实际上不需要从 id 远程 .'s 。您只需要转义选择器中的任何特殊字符。例如,要选择以下元素:
<div id='foo.bar'></div>
... you can use:
... 您可以使用:
$('#foo\.bar');