如何在js中创建多行字符串

我尝试发送 HTML,但在将内容键的值作为多行字符串写入时出现错误。这个的有效语法是什么?


var data = {    

        "subject":"Test",

        "content": "{<body><br><br><table style='text-align: left; border: 10px solid

                   #f5f5f5;padding:36px;margin:0px auto;background-color: #fff;'

                   class='maincontainer' cellspacing='0 cellpadding='0'>}"

      };

但它显示错误。如何纠正?


杨魅力
浏览 159回答 5
5回答

至尊宝的传说

ECMAScript 6 (ES6) 引入了一种新的文字类型,即模板文字。它们有很多特征,变量插值等,但对于这个问题最重要的是,它们可以是多行的。模板文字由反引号分隔:var html = `  <div>    <span>Some HTML here</span>  </div>`;

翻过高山走不出你

您可以为此使用字符串连接。像这样 :var data = {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "subject":"Test",&nbsp; &nbsp; &nbsp; &nbsp; "content":"{<body><br><br><table style='text-align: left; border: 10px solid" +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" #f5f5f5;padding:36px;margin:0px auto;background-color: #fff;'" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" class='maincontainer' cellspacing='0 cellpadding='0'>}"&nbsp; &nbsp; &nbsp; };

繁星coding

var data = {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; "subject":"Test",&nbsp; &nbsp; "content":'<body><br><br><table style=\'text-align: left; border: 10px solid\n' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '#f5f5f5;padding:36px;margin:0px auto;background-color: #fff;\'\n' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class=\'maincontainer\' cellspacing=\'0 cellpadding=\'0\'>'&nbsp; };通过这种方式,您可以获得正确的 reesult.worked for me。

慕森卡

首先你的问题不清楚。但我正在努力理解它。您需要稍后将字符串插入到 html 标记中。ECMAScript 6 (ES6) 引入了一种新的文字类型,即模板文字。所以将您的代码更改为。var data = {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "subject":"Test",&nbsp; &nbsp; &nbsp; &nbsp; "content":`{<body><br><br><table style='text-align: left; border: 10px solid" +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" #f5f5f5;padding:36px;margin:0px auto;background-color: #fff;'" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" class='maincontainer' cellspacing='0 cellpadding='0'>}`&nbsp; &nbsp; &nbsp; };现在如果你想在你的 html 字符串中添加变量.. exvar test = 'hello world';// variable inside your html stringvar html = `<div class="new_class">${test}</div>`

智慧大石

//&nbsp; This is how we can create multiline strings in js// 1. by using (\n) adding line space&nbsp;// 2. by using (+) concaginate two strings&nbsp;let string = "Hello , This is Pranav. Are you doing well ? \nAnd here i am creating a multiline string in js"console.log(string)let string2 = "Hello , This is Pranav. Are you doing well ?" + " And here i am creating a multiline string in js"console.log(string2)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript