简体   繁体   中英

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!"

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

Or use string interpolation, where you can have both without escaping.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM