全部。我是 JavaScript 新手,所以希望这对大家来说是一个非常简单的问题。但我绝对,为了我的一生,无法弄清楚如何做到这一点!我正在创建一个时间表程序,我需要输出看起来像这样:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
...等等。但是,每当它输出到屏幕时,它只显示循环的最后一个输出。所以它将显示“5 x 12 = 60”。每次程序通过循环时,我都需要它来显示每个单独的输出。我该怎么做呢?
非常感谢提前!
<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta charset="utf-8">
<!--
Input
User clicks the "Compute Table" button.
Processing
The computer creates a times table based on the users' input.
Output
Outputs the table to the user.
-->
<title>Times Table</title>
<script>
function computeTable() {
// Declaring some variables and pulling the integer from the HTML form. Nothing to see here.
var integer = parseInt(document.getElementById('input').value);
var i = 1;
// Running the loop and doing all the arithmetic
while (i < 12) {
i++;
}
// This line displays the output to the user
var output = document.getElementById('outputdiv');
output.innerHTML = integer + " x " + i + " = " + (integer * i);
}
</script>
</head>
<body>
<h1>Times Table</h1>
Please enter a positive integer: <input type="text" id="input">
<button type="button" onclick="computeTable()">Compute Table</button>
<hr>
<div id="outputdiv" style="font-weight:bold"></div>
</body>
</html>