在文档编写(JavaScript)中创建新行
Create new line in document write (JavaScript)
如何将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>
把'n
放在引号里,就像这样:"'n"
,不要忘记在它们后面加加号。这是一个连接在一起的大字符串。
ETA:正如在另一个答案中提到的,纯HTML不支持换行,所以使用<br>
!
您需要:
- 引用所有字符串文字
- 在要连接的每个字符串对之间放置一个
+
- 使用
<br>
元素而不是文字新行(因为HTML中的新行不会导致换行)
例如:
"Second Value Entered: " + num2 + "<br>" +
也就是说,这看起来更像是dl
/dt
/dd
的作业,而不是一个有换行符的段落
<!DOCTYPE html>
<!-- JavaScript Arith - Oct. 22, 1918 -->
var name = prompt("Hello and Welcome! I'm Chip. We're going to play a little math game. What's your name?", "");
alert("Hello, " + name + ". Please enter a number for each prompt. Please, do not use 0 for either number.")
var answer, sum, difference, product, quotient, power = -1; // Initialize
// Input - Prompt for the numbers
// Processing - the preparation for number crunching
// Make sure the values are numeric.var first = Number(prompt("Enter the first number", ""));
var first = Number(prompt("Enter the first number", ""));
var second = Number(prompt("Enter the second number", ""));
// Output - results
// Feedback - later for errors, e.g., 0, letters, etc.
document.write(name + ", the numbers chosen were: " + first + " and " + second);
document.write(".<br>The results are shown below.<ul>");
var sum = first + second;
document.write("<li>Sum: " + sum + "</li>"); // ad d
var difference = first - second;
document.write("<li>Difference: " + difference + "</li>"); // subtract
/*
M, S and D. For positive arguments M and S holds:
The minuend (M) is the first number in a subtraction; the number from which another number (the Subtrahend) is to be subtracted.
The subtrahend (s) is the number that is being subtracted from the minuend.
Minuend.
Example: in 8 − 3 = 5, 8 is the minuend (M). 3 is the subtrahend (S). 5 is the difference (D).
If the minuend is larger than the subtrahend, the difference D is positive.
If the minuend is smaller than the subtrahend, the difference D is negative.
In any case, if minuend and subtrahend are equal, the difference D = 0.
*/
var product = first * second;
document.write("<li>Product: " + product + "</li>"); // multiply
/* factors */
var quotient = first / second;
document.write("<li>Quotient: " + quotient + "</li>"); // divide
/*
Division shows the relationship between the dividend, divisor, quotient and remainder. The number which we divide (before the '/' operator) is called the 'dividend.' The number by which we divide (after the '/' operator)is called the 'divisor.' The result obtained is called the 'quotient.'
*/
var power = first ** second;
document.write("<li>Power: " + power + "</li>"); // exponentiation
document.write("</ul>Thanks, " + name + ". Have a Nice Day!");
document.write("'n");
document.write("<br>");
function breakline()
{
document.write("Hi?<br> nice shirt");
// "<br>" use in document.write
alert("Hi? 'n nice shirt");
// '/n' use in alert
}
breakline();
相关文章:
- 字符串中的
标记未正确在 HTML 中创建新行 - 使用删除文本创建新行
- Razor代码在JavaScript字符串中创建新行
- 输入键以创建新行并聚焦当前输入字段
- 主干更新输入创建新行
- 为什么“”即使被告知不要创建新行
- 如何使用引导程序在一行中存在 3 个缩略图后创建新行
- Javascript 每 n 个,创建新行
- 按回车键时,文本区域不会停止创建新行
- 在文档编写(JavaScript)中创建新行
- 在JavaScript中,换行符实际上并不是在创建新行,有没有其他选择,或者我做错了什么
- 为什么“;innerHTML”;具有<br>不会't创建新行
- 当单击在表中创建新行时,它只会短暂出现
- 如何使用Javascript在HTML中的表上创建新行
- JavaScript在HTML中创建新行,onclick将值粘贴在另一个的下面
- 如何在输出中创建新行(JavaScript Gui)
- 当在控制台中跟踪变量时,如何创建新行
- 如果没有焦点的文本框中有数据,则创建新行,并且在第二行下拉列表中不提供数据
- 如何在角函数ng-repeat中创建新行
- Jquery在创建新行时不向最后一行添加索引