Javascript 用 enter 分隔字符串
Javascript separate string with enter
我想把 html 脚本放在 <div id>
里面,但是我不能用 enter 分隔字符串。所以我把这样的代码(例子):
var html = "<table style='width:100%'><tr><th>Firstname</th><th>Lastname</th><th>Age</th></tr>";
$("#panelChatlog").html(this.html);
如您所见,字符串是 html 代码,但我想将 html 代码分开,这样可读性更好:
var html = "
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
";
$("#panelChatlog").html(this.html);
但在我的 javascript 中是不可能的。我正在使用 processmaker 3.x
有什么建议吗?
这就是引入 ` 字符的目的,因此您无需在字符串中手动写入 \n
。只需替换引号就足够了(确保关闭您的 table
标签):
var html = `
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
</table>
`;
alert(html);
你可以使用ES6中引入的template literals。
const htmlMarkup = `
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
</table>`
使用@Christian 所说的`字符和用户trim 函数从HTML 代码中删除空格。
var html = `
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
`;
$("#panelChatlog").html(html.trim());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div id="panelChatlog">
</div>
我想把 html 脚本放在 <div id>
里面,但是我不能用 enter 分隔字符串。所以我把这样的代码(例子):
var html = "<table style='width:100%'><tr><th>Firstname</th><th>Lastname</th><th>Age</th></tr>";
$("#panelChatlog").html(this.html);
如您所见,字符串是 html 代码,但我想将 html 代码分开,这样可读性更好:
var html = "
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
";
$("#panelChatlog").html(this.html);
但在我的 javascript 中是不可能的。我正在使用 processmaker 3.x 有什么建议吗?
这就是引入 ` 字符的目的,因此您无需在字符串中手动写入 \n
。只需替换引号就足够了(确保关闭您的 table
标签):
var html = `
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
</table>
`;
alert(html);
你可以使用ES6中引入的template literals。
const htmlMarkup = `
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
</table>`
使用@Christian 所说的`字符和用户trim 函数从HTML 代码中删除空格。
var html = `
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
`;
$("#panelChatlog").html(html.trim());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div id="panelChatlog">
</div>