[英]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.