我對這一切都很陌生,我需要創建一個 JavaScript for 回圈,當單擊一個按鈕時列印出數字 1 到 10,當單擊另一個按鈕時列印出 -1 到 -10,如附加的螢屏截圖所示。

我已經這樣做了,但我很困。
<!DOCTYPE html>
<html>
<body>
<h1>For loop statement exercise</h1>
<p>Press PLUS to display 1 to 10:
<button onclick="plus()">PLUS</button>
<p>Press MINUS to display -1 to -10:
<button onclick="myFunction()">MINUS</button>
<p id="i"></p>
<script>
for (i = 1; i <= 10; i )
{
document.write("i" < br > );
}
</script>
</body>
</html>
uj5u.com熱心網友回復:
試試這個
<!DOCTYPE html>
<html>
<body>
<h1>For loop statement exercise</h1>
<p>
Press PLUS to display 1 to 10: <button onclick="plus()">PLUS</button>
</p>
<p>
Press MINUS to display -1 to -10:
<button onclick="minus()">MINUS</button>
</p>
<p id="i"></p>
<script>
function minus() {
for (i = -1; i >= -10; i--) {
document.getElementById("i").innerHTML =
document.getElementById("i").innerHTML `${i} <br>`;
}
}
function plus() {
for (i = 1; i <= 10; i ) {
document.getElementById("i").innerHTML =
document.getElementById("i").innerHTML `${i} <br>`;
}
}
</script>
</body>
</html>
uj5u.com熱心網友回復:
我不知道您是否希望每次單擊時都添加一個數字,或者只需單擊一下即可添加所有數字,但我這樣做了:
<!DOCTYPE html>
<html>
<body>
<h1>For loop statement exercise</h1>
<p>Press PLUS to display 1 to 10:</p>
<button onclick="plus()">PLUS</button>
<p id=" "></p>
<p>Press MINUS to display -1 to -10:</p>
<button onclick="minus()">MINUS</button>
<p id="-"></p>
<script>
function plus() {
for(let i = 1; i <= 10; i ) {
document.getElementById(" ").innerHTML = i " ";
}
}
function minus() {
for(let i = -1; i >= -10; i--) {
document.getElementById("-").innerHTML = i " ";
}
}
</script>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/390844.html
標籤:javascript for循环
上一篇:為什么這個for回圈只輸出一次?
下一篇:函式包含無限回圈,看起來一切正常
