admin管理员组

文章数量:1323723

I want it to display dice image but no output? What's wrong? I choose a random number from 1-6 and it will show the face of the dice. Well it is supposed to but it's not working. I also want to display the images elsewhere not replacing the button. How do you do that?

<html>
<head><title>DiceBoy</title>
</head>
<body>
<script>
function getRandom(){
var RandomNumber1 = Math.floor(Math.random() * 6) + 1;
var RandomNumber2 = Math.floor(Math.random() * 6) + 1;
  if(RandomNumber1 == 1) document.write('<img src="dice1.png">');
  else if(RandomNumber1 == 2)  document.write('<img src="dice2.png">');
  else if(RandomNumber1 == 3)  document.write('<img src="dice3.png">');
    else if(RandomNumber1 == 4)  document.write('<img src="dice4.png">');
      else if(RandomNumber1 == 5)  document.write('<img src="dice5.png">');

                  else var  document.write('<img src="dice6.png">');


}

</script>
<input type="button" value="Throw Dices" onClick="getRandom();">

I want it to display dice image but no output? What's wrong? I choose a random number from 1-6 and it will show the face of the dice. Well it is supposed to but it's not working. I also want to display the images elsewhere not replacing the button. How do you do that?

<html>
<head><title>DiceBoy</title>
</head>
<body>
<script>
function getRandom(){
var RandomNumber1 = Math.floor(Math.random() * 6) + 1;
var RandomNumber2 = Math.floor(Math.random() * 6) + 1;
  if(RandomNumber1 == 1) document.write('<img src="dice1.png">');
  else if(RandomNumber1 == 2)  document.write('<img src="dice2.png">');
  else if(RandomNumber1 == 3)  document.write('<img src="dice3.png">');
    else if(RandomNumber1 == 4)  document.write('<img src="dice4.png">');
      else if(RandomNumber1 == 5)  document.write('<img src="dice5.png">');

                  else var  document.write('<img src="dice6.png">');


}

</script>
<input type="button" value="Throw Dices" onClick="getRandom();">
Share Improve this question edited Jan 31, 2013 at 10:09 Asmastas Maz asked Jan 31, 2013 at 10:05 Asmastas MazAsmastas Maz 3973 gold badges7 silver badges17 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 6

This code looks awful, make sure you always DRY code:

function getRandom(){
     var RandomNumber1 = Math.floor(Math.random() * 6) + 1;
     var RandomNumber2 = Math.floor(Math.random() * 6) + 1;

     document.getElementById('result').innerHTML = '<img src="dice' + RandomNumber1 + '.png">';
     document.getElementById('result').innerHTML += '<img src="dice' + RandomNumber2 + '.png">';
}

http://jsfiddle/tzrGh/4/

That var is unnecessary. Remove it and it works.

http://jsfiddle/rRhqn/

Next time, open your Javascript console (Ctrl+shift+J in Chrome for example) and read the errors that e up.


To have more control over your document, drop document.write and use the document object model.

Have a look at this function:

function writeDice(n) {
    var img = document.createElement('img');
    img.src = 'dice'+n+'.png';
    document.getElementById('dice_container').appendChild(img);
}

It creates an img element, sets its src depending on the parameter and appends it to an element that has the ID dice_container.

In your HTML, set this ID on the place where you want the images to appear.

Demo: http://jsfiddle/REPAe/

You have an extra var at the end

else document.write('<img src="dice6.png">'); //removed the var 

You have an extra var on the last else

and for your wish to show the dices else where I will continue Cristy great implemenatation.

add an enpty div under the button:

<input type="button" value="Throw Dices" onClick="getRandom();">
<div id="diceImage1"> </div>
<div id="diceImage2"> </div>

and then use this function:

function getRandom(){
  var RandomNumber1 = Math.floor(Math.random() * 6) + 1;
  var RandomNumber2 = Math.floor(Math.random() * 6) + 1;

  document.getElementById("diceImage1").innerHTML = '<img src="dice' + RandomNumber1 + '.png">';
  document.getElementById("diceImage2").innerHTML = '<img src="dice' + RandomNumber2 + '.png">';
}

You could do like this:

function getRandom(){
  var RandomNumber1 = Math.floor(Math.random() * 6) + 1;
  document.getElementById("showImage").innerHTML='<img src="dice' + RandomNumber1 + '.png">';
}

and in the markup

before your button:

<div id="showImage"></div>

In dicee.html

<body onload="getRandom()">

In index.js

2 Approaches:

  1. Easy one (using direct src);
function getRandom(){
  var randomNumber1 = Math.floor(Math.random() * 6) + 1;
  var randomNumber2 = Math.floor(Math.random() * 6) + 1;

  document.querySelector(".img1").src="images/dice"+randomNumber1+".png";

  document.querySelector(".img2").src="images/dice"+randomNumber2+".png";
}
  1. According to the question (using setAttribute)
function getRandom(){
  var randomNumber1 = Math.floor(Math.random() * 6) + 1;
  var randomNumber2 = Math.floor(Math.random() * 6) + 1;

 document.querySelector(".img1").setAttribute("src","images/dice"+randomNumber1+".png");
 document.querySelector(".img2").setAttribute("src","images/dice"+randomNumber2+".png");
}

本文标签: htmlJavascript DiceStack Overflow