如何在jQuery中转义单引号? [英] How do I escape a single quote in jQuery?

查看:721
本文介绍了如何在jQuery中转义单引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用转义功能来转义单引号:

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");

转义函数将单引号用%27替换为"Workers%27_Compensation".

The escape function replaces the single quote with %27 to "Workers%27_Compensation".

所以我得到一个错误,

Microsoft JScript运行时错误:语法错误,无法识别的表达式:#Workers%27_Compensation.tag

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

推荐答案

使用反斜杠

"Workers\'_Compensation";

在选择器内部,您将需要其中的2个 "Workers\\'_Compensation";

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

检查小提琴

Check Fiddle

这篇关于如何在jQuery中转义单引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆