如何在 jQuery 中转义单引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16845869/
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
How do I escape a single quote in jQuery?
提问by skwrox
I am trying to use the escape function to escape a single quote:
我正在尝试使用转义函数来转义单引号:
var tagDesc = "Workers'_Compensation";
tagDesc = escape(tagDesc);
$("#" + tagDesc + ".tag").css("display", "none");
The escape function replaces the single quote with %27
to "Workers%27_Compensation"
.
转义函数用%27
to替换单引号"Workers%27_Compensation"
。
So I get an error,
所以我得到一个错误,
Microsoft JScript runtime error: Syntax error, unrecognized expression: #Workers%27_Compensation.tag
Microsoft JScript 运行时错误:语法错误,无法识别的表达式:#Workers%27_Compensation.tag
回答by Sushanth --
Use backslash
使用反斜杠
"Workers\'_Compensation";
"Workers\'_Compensation";
Inside a selector you would require 2 of them"Workers\\'_Compensation";
在选择器中,您需要其中的 2 个"Workers\\'_Compensation";
回答by Alessandro Minoccheri
jQuery is JavaScript and to escape a special character you can use backslash.
jQuery 是 JavaScript,要转义特殊字符,您可以使用反斜杠。
With \
you can escape '
Try this:
有了\
你能逃脱'
试试这个:
var tagDesc = "Workers\'_Compensation";
tagDesc = escape(tagDesc);
$("#" + tagDesc + ".tag").css("display", "none");