如何在Javascript变量中添加单引号? [英] How to add single quote in the variable in Javascript?

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

问题描述

我的变量 var str 如下:

var str = <option value="1">tea</option>;

我想做到以下

var quote_str = '<option value="1">tea</option>;'

有没有人可以帮助我?预先感谢!

Is there anyone can help me? Thanks in advance!

我尝试了以下代码,但这是不正确的.

I have tried the following code,however, it's not correct.

var quote_str =  'str';

推荐答案

我认为您想在字符串文字之外使用分号:

I think that you want the semicolon outside the string literal:

var quote_str = '<option value="1">tea</option>';

如果您还希望在字符串中包含撇号字符,则可以使用 \'将撇号放在由撇号分隔的字符串中:

If you mean that you want apostrophe characters inside the string also, you can use \' to put an apostrophe in a string delimited by apostrophes:

var quote_str = '\'<option value="1">tea</option>\'';

您还可以使用引号分隔字符串.然后,您不必转义撇号,但是您必须转义引号:

You can also use quotation marks to delimit the string. Then you don't have to escape the apostrophes, but you have to escape the quotation marks:

var quote_str = "'<option value=\"1\">tea</option>'";

如果您已经有一个字符串,并且想在字符串周围添加撇号,则可以将字符串连接起来:

If you already have a string, and want to add apostrophes around it, you concatenate strings:

var quote_str =  "'" + str + "'";

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

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