How do I escape a single quote in jQuery? [duplicate]

一笑奈何 提交于 2019-12-06 02:32:46

问题


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".

So I get an error,

Microsoft JScript runtime error: Syntax error, unrecognized expression: #Workers%27_Compensation.tag


回答1:


Use backslash

"Workers\'_Compensation";

Inside a selector you would require 2 of them "Workers\\'_Compensation";

Check Fiddle




回答2:


jQuery is JavaScript and to escape a special character you can use backslash.

With \ you can escape '
Try this:

var tagDesc = "Workers\\'_Compensation";
tagDesc = escape(tagDesc);
$("#" + tagDesc + ".tag").css("display", "none");


来源:https://stackoverflow.com/questions/16845869/how-do-i-escape-a-single-quote-in-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!