简体   繁体   English

如何在JavaScript的文本提示中显示单引号和双引号?

[英]How do I show single and double quotes in a text alert in javascript?

I want a javascript alert that reads, "It's "Hammer" time!" 我想要一个JavaScript警报,上面写着“现在是“锤子”时间!”

What is the best code to write this? 编写此代码的最佳代码是什么?

尽管您可以使用带有'并转义'的字符串,或带有"并转义"的字符串,但是最好使用模板文字,因为它的定界符是反引号,所以它不需要转义引号:

 alert(`"It's "Hammer" time!"`); 

为了显示单引号或双引号,您可以像以下alert("\\"It's \\"Hammer\\" time!\\"")一样编写代码alert("\\"It's \\"Hammer\\" time!\\"")

请从开头和结尾转义"s

 alert("\\"It's \\"Hammer\\" time!\\"") 

You need to escape it using \\ 您需要使用\\对其进行转义

For example: 例如:

alert("Something \"here\"!!");

How to display double quotes in JavaScript 如何在JavaScript中显示双引号

Or use string interpolation, where you can have both without escaping. 或使用字符串插值法,在两种情况下都可以避免转义。

alert(`Hello, I'm "nobody"!`);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中将单引号括在双引号中 - How do i enclose the Single quotes in double quotes in javascript 如何在 JavaScript 中用双引号替换单引号? - How do I replace single quotes with double quotes in JavaScript? 如何将带单引号的字符串转换为double用于json解析 - How do I convert a string with single quotes into double for json parse 为什么在JavaScript中单引号中必须有双引号? - Why do I have to have double quotes inside single quotes in JavaScript? 如何从javascript输入的文本中转义双引号? - How can I escape double quotes from a text input in javascript? 如何在Javascript数组中替换单引号而不是双引号? - How to replace single quotes instead of double quotes in array of Javascript? 如何在 PHP/Javascript 中转义双引号、双引号内、单引号内? - How to escape a double quotes, inside a double quotes, inside a single quotes in PHP/Javascript? 如何在javascript的html文本区域中显示单引号,双引号,换行符等字符 - How to allow showing caharcaters like single quote, double quotes, new line charcters in html text area in javascript 如何在JavaScript代码中的字符串中显示单引号? - How to show single quotes in string in javascript code? 如何在jquery的append(),onclick(),alert()中一次转义单引号或双引号? - How to escape single quotes or double quotes within jquery's append(), onclick(), alert() at one time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM