双引号和单引号之间的区别 [英] Difference between double and single quotes

查看:132
本文介绍了双引号和单引号之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC应用程序,我在其中使用jQuery将一些文本插入DOM.

如果我使用:

$("#toggle").text('<%: translated.Show %>');

将呈现文本.

如果我使用:

$("#toggle").text("<%: translated.Show %>");

如果资源字符串用双引号引起来,则会引发JavaScript错误,请注意,单引号将工作版本引起来,双引号将错误版本引起来.

谁能解释为什么,我认为Java中的单引号和双引号之间没有什么区别.

"translated.Show"字符串不包含只包含纯文本的引号.

解决方案

JS中双引号和单引号之间的唯一区别是,在单引号内,必须对单引号进行转义,而在双引号内,必须对双引号进行转义. /p>

问题很可能是translated.Show中的数据包含双引号(尽管您保证不会...将引号字符 视为纯文本的一部分).

确保您正在关注在将不受信任的数据插入JavaScript数据之前

I have an MVC app where I'm inserting some text into the DOM using jQuery.

If I use:

$("#toggle").text('<%: translated.Show %>');

The text is rendered.

If I use:

$("#toggle").text("<%: translated.Show %>");

Where resource string is wrapping in double quotes it throws a JavaScript error, note the single quotes wrapping the working version and double quotes wrapping the erroneous version.

Can anyone explain why, I thought there was little difference between single and double quotes in Javascript.

The 'translated.Show' string does not contain quotes just plain text.

解决方案

The only difference between double and single quotes in JS is that inside single quotes, single quotes must be escaped while inside double quotes, double quotes must be escaped.

The problem is most likely that the data inside translated.Show contains a double quote (despite your assurances that it does not… quote characters are considered part of plain text).

Make sure your are following Rule 3: JavaScript Escape Before Inserting Untrusted Data into JavaScript Data

这篇关于双引号和单引号之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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