Javascript [ document.write ("___"); ] 不以 document.write 开始每一行的快捷方式 ("
Javacript [ document.write ("___"); ] Shortcut to not start each line with document.write ("
在此代码中,我想在 JavaScript 中插入一些 HTML 和普通文本,并且不希望它总是在每一行的开头输入 document.write("
,例如:
document.write("some text
some text
some text");
而不是这个:
document.write("Hello World!");
document.write("Have a nice day!");
document.write("<br>");
document.write("<p>some random paragraph");
document.write("Hello World!");
document.write("Have a nice day!");
document.write("<br>");
document.write("<p>some random paragraph");
⚠ Warning: Use of the document.write() method is strongly discouraged.
- 使用 template/string 创建 HTML,然后将其分配给文档正文(或您想要的任何元素)的
innerHTML
。
document.body.innerHTML = `
Hello World!<br />
Have a nice day!<br />
<p>some random paragraph</p>
`;
⚠警告:不要使用document.write
而是使用 document.body.innerHTML = '';
document.body.innerHTML = `
your text
<br>
your text
`;
在此代码中,我想在 JavaScript 中插入一些 HTML 和普通文本,并且不希望它总是在每一行的开头输入 document.write("
,例如:
document.write("some text
some text
some text");
而不是这个:
document.write("Hello World!");
document.write("Have a nice day!");
document.write("<br>");
document.write("<p>some random paragraph");
document.write("Hello World!");
document.write("Have a nice day!");
document.write("<br>");
document.write("<p>some random paragraph");
⚠ Warning: Use of the document.write() method is strongly discouraged.
- 使用 template/string 创建 HTML,然后将其分配给文档正文(或您想要的任何元素)的
innerHTML
。
document.body.innerHTML = `
Hello World!<br />
Have a nice day!<br />
<p>some random paragraph</p>
`;
⚠警告:不要使用document.write
而是使用 document.body.innerHTML = '';
document.body.innerHTML = `
your text
<br>
your text
`;