【发布时间】:2014-12-26 23:11:58
【问题描述】:
如何使 document.get 中的所有项目换行?我试过br/ 和\n。两者都不起作用。它们要么导致 JavaScript 运行不正确,要么不创建新行。我知道我可以将每一个都放在自己的段落元素中,但出于我的目的,我必须将它们全部放在一个段落元素中。
<script type="text/javascript">
<!--
var firstString = prompt(" Enter the first number ", "");
var secondString = prompt(" Enter the second number ", "");
var num1 = parseFloat(firstString);
var num2 = parseFloat(secondString);
var addition = num1 + num2; // Addition of num1 and num2
var subtraction = num1 - num2; // Subtraction of num1 and num2
var multiplication = num1 * num2; // Multiplication of num1 and num2
var division = num1 / num2 // Division of num1 and num2
//These are the ones I'm trying to get to appear on separate lines
document.write (
"<p> First Value Entered: " + num1 + \n
"Second Value Entered: " + num2 + \n
"Sum: " + addition + \n
"Difference: " + subtraction + \n
"Product: " + multiplication + \n
"Quotient: " + division + "</p>"
);
// -->
</script>
【问题讨论】:
-
不要使用
document.write,它是在我们拥有 DOM 操作函数之前的古老 JS API 的一部分,永远不应该使用(它很危险:it really doesn't do what you think it does)。用document.createElement创建你的东西,用.textContent=...设置它们的内容(不是.innerHTML,因为那只是更多的DOM内容,就这样创建)然后用document.body.appendChild(...)/element.appendChild(...)将它添加到DOM中跨度> -
是的,我知道,这是作业的标准-_-
-
你需要和分配它的人谈谈。这不是正确的 HTML+JS,而且已经很多年没有了。
-
为什么不用
这里的文字\n更多文字
?
标签: javascript html newline line-breaks