双嵌套引号 [英] Double nested quotes

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

问题描述

我有这行代码

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要求使用单引号-答案就是单引号和双引号,反之亦然.

In the above question, the OP asks for single-nested quotes - single quotes isnide double quotes was the answer or vice versa.

在我的问题中,我要求双引号-在[quotes]内[quotes]内的[quotes].我的问题是额外的引号层.

In my question, I ask for double-nested quotes - [quotes] inside [quotes] inside [quotes]. My question is an extra layer of 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值,因此您永远不必转义apostrophesquotes:

But, in such cases it's better to use the backtick to contain the innerHTML value, thus you'd never need to escape the apostrophes nor the quotes:

formsParent.innerHTML = `<p style='color: black; font-family: "Times New Roman"; font-size: 2em'> Order submitted. Thank you for ordering! </p>`;

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

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