gpt4 book ai didi

javascript - 如何使用 Javascript append 网页

转载 作者:行者123 更新时间:2023-11-28 21:10:46 24 4
gpt4 key购买 nike

这里还有另一个 JavaScript 菜鸟问题。我有以下代码,但我想要一种方法,每次玩家输或赢时,都在 Id 'lost' 和 'score' 后面 append 一个“I”(例如,你猜对了 3 次,猜错了 4 次)所以胜利时会出现“III”,失败时会出现“IIII”)。如何用javascript最好地做到这一点?

<html>
<head>
<title>Sarah's Card Game</title>
<script type="text/javascript">
Array.prototype.shuffle = function() {
var len = this.length;
var i = len;
while (i--) {
var p = parseInt(Math.random()*len);
var t = this[i];
this[i] = this[p];
this[p] = t;
}
};
</script>
</head>
<body>
<body onLoad="number=0"></p>
<p>This is the fun card game, a number will appear, afterwards, guess the card below which matches the number shown!</p>

<div id="click"><button onCLick="Choose()">Button</button></div>
<div id="random"></div>
<div id="score"></div>
<div id="lost"></div>
<script type="text/javascript">
function Choose() {
var array = [1,2,3,4];
array.shuffle();
number = array[0];
document.getElementById("random").innerHTML=("Number is: " + number);
}

</script>
<div id="cards">
<button onClick="Picker()">Card 1</button>
<button onClick="Picker()">Card 2</button>
<button onClick="Picker()">Card 3</button>
<button onClick="Picker()">Card 4</button>
<script type="text/javascript">
function Picker() {
var array2 = [1,2,3,4];
array2.shuffle();
number2 = array2[0];
if (number==0) {
alert('please pick a number first!');
}
else if (number2==number) {
alert('you won!');
var wins = document.getElementById('score');
wins.lastChild.nodeValue = wins.lastChild.nodeValue+ document.getElementById('score').value;
}
else if (number2!=number){
alert('you lost!');
var loses = document.getElementById('lost');
loses.lastChild.nodeValue = loses.lastChild.nodeValue+ document.getElementById('lost').value;
}
}

</script>
</div>

最佳答案

document.getElementById("score").innerHTML += "I";

关于javascript - 如何使用 Javascript append 网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8747988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com