if语句与getElementById不起作用

时间:2014-10-28 00:31:45

标签: javascript getelementbyid

如果我尝试使用if语句与docoument.getElementById(id).src ==它不起作用,但只使用document.getElementById()。src =它的工作原理...我没有得到任何错误或任何事情我点击图片。

 //  dice function
function keepDice(id){


//document.getElementById(id).src="diceX1.gif"; <<<<<<<<<< works... 

if(document.getElementById(id).src=="dice1.gif") document.getElementById(id).src="diceX1.gif";
else if(document.getElementById(id).src=="dice2.gif") document.getElementById(id).src="diceX2.gif";
else if(document.getElementById(id).src=="dice2.gif") document.getElementById(id).src="diceX3.gif";
else if(document.getElementById(id).src=="dice3.gif") document.getElementById(id).src="diceX4.gif";
else if(document.getElementById(id).src=="dice4.gif") document.getElementById(id).src="diceX5.gif";
else if(document.getElementById(id).src=="dice5.gif") document.getElementById(id).src="diceX6.gif";

}

functionfunction roll()
    {

    var diceSum=0; 


    if (countClicks()==true){
    for(i=0; i<5; i++)

    {

    d[i] = Math.floor(Math.random()*6)+1;
    document.getElementById("d"+i).src="dice"+d[i]+".gif";
    diceSum+=d[i];

    } 



    if (/1234|2345|3456/.test(d.join("").replace(/(.)\1/,"$1"))) alert("small straight");
    else if (/12345|23456/.test(d.join("").replace(/(.)\1/,"$1"))) alert("large straight");
    else if (/(.)\1{2}(.)\2|(.)\3(.)\4{2}/.test(d.join("").replace(/(.)\1/,"$1")))    alert("full house");

        ofAKindScore=diceSum;
        document.getElementById("rolls").value=ClickCount;
        diceSum=0;     
    }

    for (i = 1; i < 15; i++){


        document.getElementById("s"+i).value='';
        taken[i]= false;

        }


    }

<div style="border: solid 3px #0000ff">
<img  id='d0' onClick="keepDice('d0')" src="dice0.gif">

<img  id='d1' onClick="keepDice('d1')" src="dice0.gif">

<img  id='d2' onClick="keepDice('d2')" src="dice0.gif">

<img  id='d3' onclick="keepDice('d3')" src="dice0.gif">

<img  id='d4' onClick="keepDice('d4')" src="dice0.gif">

<p> Rolls Left: <input id='rolls' type='text' value='3' size="3"></p>
</div>

1 个答案:

答案 0 :(得分:3)

if语句正在寻找dice1.gifdice2.gifdice3.gif等。但所有图片都以dice0.gif开头。代码永远不会检查dice0.gif