如何使用Javascript附加网页

时间:2012-01-05 18:46:04

标签: javascript append

这里的另一个noob javascript问题。我有以下代码,但我希望有一种方法可以在每次玩家失败或获胜时将Ids'丢失'和'得分'与“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>

1 个答案:

答案 0 :(得分:3)

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