在jQuery或JavaScript中转义单引号 [英] Escape single quotes in jQuery or JavaScript
本文介绍了在jQuery或JavaScript中转义单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为< span>
标记显示以下文字。如何转义以下单引号?
I want to display the following text for the <span>
tag. How do I escape the single quotes for the following?
$("#spn_err").text($('#txt1').attr('value')+" is not valid");
我想将消息显示为' ZZZ'无效
。这里 $('#txt1')。attr('value')
是一个动态值。它可能有 abc
, bcc
, ddd
, ZZZ
。我该怎么做?
I want to display the message as 'ZZZ' is not valid
. Here $('#txt1').attr('value')
is a dynamic value. It may have abc
, bcc
, ddd
, zzz
. How can I do this?
推荐答案
像这样:
$("#spn_err").text("'" + $('#txt1').val() + "' is not valid");
在双引号内,单引号是普通字符,反之亦然。否则你可以通过添加一个反斜杠来逃避它们:\
或'\''
。
Inside double quotes, single quotes are normal characters and vice versa. Otherwise you can escape them by prepending a backslash: "\""
or '\''
.
这篇关于在jQuery或JavaScript中转义单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文