问题
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