将图像添加到Javascript代码

时间:2014-07-31 00:25:34

标签: javascript html5 image

是的,我正在尝试为javascript创建这个游戏并自定义它,我让游戏正常工作,这是你的数字游戏的基本猜测我想要做的是弄清楚如何让我的输出成为一个图像而不是然后是文字。这是我的代码。

  <script type="text/javascript">




    function yourGuess() {
    guess = document.getElementById("guess").value;
    guesses = document.getElementById("output");

    if (guess == numToGuess) {
        guesses.value = guesses.value + "\r" + "You have guessed correctly! ("+guess+")";
    } else if (guess > numToGuess) {
        guesses.value = guesses.value + "\r" + "Your guessing too high!("+guess+")";

    } else {
        guesses.value = guesses.value + "\r" + "Your guessing too low!("+guess+")";
    }
    }

    var numToGuess = Math.floor(Math.random()*100);
    </script>

    <fieldset style="float: left; width: 380px; background-color:pink;">
     <legend >Inputs</legend>
    <label for="guess">Your Guess:</label>
 <input type="text" id="guess" value="Between 1-100" />
 <input type="button" onclick="yourGuess()" value="submit" /><br />
 <input type="button" onclick="" value="Show My Guesses" />  
 <input type="button" onclick="" value="New Game" /><br />
 </fieldset>

<fieldset style="float: left; width: 380px; background-color:red;">
 <legend style="background-color:Lime; color:Blue;"> Output </legend>
 <textarea id="output" name="output" rows="5" style="width: 100%;"></textarea>
</fieldset>

这是一个Jsfiddle链接:http://jsfiddle.net/92PCR/2/

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

就像添加图像元素一样简单:

<img id="outputimg" src="//:0" alt="output" />

然后改变该图像的来源:

document.getElementById("outputimg").src = "yourNumber.gif";