问题描述
我有这行代码
formsParent.innerHTML = "<p style = 'color: black; font-family: "Times New Roman" font-size: 2em'> Order submitted. Thank you for ordering! </p>"
第一个引号用于innerHTML
属性.接下来是<p>
元素的style
属性内的属性,最后我需要在该内部为font-family
属性提供另一个引号,其值包含多个单词,因此也需要引号.只有" 和'',并且字体家族使用双引号会引发错误.如何在引号内使用引号?
The first quote is for the innerHTML
property. Next is the properties inside the style
attribute of the <p>
element, and finally I need another quote inside this for the font-family
property with a value that has multiple words so it also needs quotation marks. There are only "" and '', and using double quotes for the font-family throws an error. How do I use quotes inside quotes inside quotes?
这不是 JavaScript字符串中双引号的重复.停止举报!
This is NOT a duplicate of the Double quote in JavaScript string. Stop flagging this!
在上述问题中,OP要求使用单引号-答案就是单引号和双引号,反之亦然.
在我的问题中,我要求双引号-在[quotes]内[quotes]内的[quotes].我的问题是额外的引号层.
推荐答案
In your case you'd need to escape the quotes
:
formsParent.innerHTML = "<p style='color: black; font-family: \"Times New Roman\"; font-size: 2em'> Order submitted. Thank you for ordering! </p>";
但是,在这种情况下,最好使用backtick
包含innerHTML
值,因此您永远不必转义apostrophes
或quotes
:
formsParent.innerHTML = `<p style='color: black; font-family: "Times New Roman"; font-size: 2em'> Order submitted. Thank you for ordering! </p>`;
这篇关于双嵌套引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!