简体   繁体   English

如何通过一个onclick事件更改两个单独的图像?

[英]How can I change two separate images using through one onclick event?

I need to create a rock paper scissors simulation where the user clicks on a picture of either a rock, piece of paper or pair of scissors and an image on the same webpage displays the choice with a picture of a hand in the shape of either rock, paper or scissors. 我需要创建一个岩石剪刀模拟,用户点击一块岩石,一张纸或一把剪刀的图片,同一网页上的图像显示选择与一个岩石形状的手的图片,纸或剪刀。 Then to the left of this picture is another hand that randomly generates one of these three shapes. 然后在这张照片的左边是另一只手,随机生成这三种形状中的一种。 I have the onclick event working for the user, however I'm having trouble figuring out how to code the random generator. 我有onclick事件为用户工作,但是我无法弄清楚如何编写随机生成器的代码。 Here is my code for just one of three functions(rock,paper,scissors), however they are all formatted the same way, just with different values. 这是我的三个函数中的一个(岩石,纸张,剪刀)的代码,但它们都以相同的方式格式化,只是具有不同的值。 Thanks in advance. 提前致谢。

 var computerChoice=Math.random();



 function clickRock(computerChoice) {
       img= document.getElementById("change1");
       img.src="leftRockHand.jpg";
       if (computerChoice < 0.3333) {
            img=document.getElementById("change2");
            img.src="rightRockHand.jpg"
       }
       else if (computerChoice >= 0.3333 && computerChoice < 0.6666) {
            img=document.getElementById("change2");
            img.src="rightPaperHand.jpg";
       }
       else if(computerChoice >= 0.6666) {
            img=document.getElementById("change2");
            img.src="rightScissorHand.jpg";

       }

    }

Here is my html 这是我的HTML

<tr>
            <td onclick="clickRock()" id="rockClick"><img src="rock.jpg"     alt="Rock"></td>

            <td rowspan="3"><img id="change1" src="leftPaperHand.jpg" alt="Left Hand" height="350"></td>

            <td rowspan="3"><img id="change2" src="rightRockHand.jpg" alt="Right Rock" height="350"></td>
        </tr>
function clickRock(choice1, choice2){
       changePicture("change1", choice1);
       changePicture("change2", choice2);
} 
function changePicture(eleId, computerChoice) {
       var img = img=document.getElementById(eleId);
       if (computerChoice < 0.3333) {
            img.src="rightRockHand.jpg"
       } 
       else if (computerChoice >= 0.3333 && computerChoice < 0.6666) {
            img.src="rightPaperHand.jpg";
       }
       else if(computerChoice >= 0.6666) {
            img.src="rightScissorHand.jpg";
       }
}

If call clickRock in click event listener. 如果在click事件监听器中调用clickRock。

Its simple use max and min to generate any number of possibilities. 它简单地使用max和min来产生任意数量的可能性。

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}

alert(getRandomInt(1, 3));

This is a generic function, not just 3 possibility, but n number. 这是一个通用函数,不仅仅是3种可能性,而是n数。 Just specify a range as min and max. 只需指定范围为最小值和最大值。

Here is a fiddle example 这是一个小提琴的例子

If you assign what Math.random() returns to that variable only once the number won't change. 如果指定Math.random()仅在数字不会更改时返回该变量。 Move that assignment into the function. 将该分配移动到该功能中。

To remove the need for less than and more than operators you can do Math.floor(Math.random() * 3) to get a number between 0 and 2. Because you are only doing an equal operation on one variable a switch statement becomes more appropriate. 为了消除对少于和多于运算符的需要,您可以执行Math.floor(Math.random()* 3)以获得介于0和2之间的数字。因为您只对一个变量执行相同的操作,所以switch语句变为更合适。

If you want to make the code even cleaner you can assign the #change2 element to the img variable only once before checking for the the random integer. 如果要使代码更清晰,可以在检查随机整数之前将#change2元素分配给img变量一次。

function clickRockAsComputer() {
    var computerChoice = Math.floor(Math.random() * 3);
    img = document.getElementById("change1");
    img.src = "leftRockHand.jpg";
    img = document.getElementById("change2");
    switch(computerChoice){
        case 0:
            img.src = "rightRockHand.jpg";
        break;
        case 1:
            img.src = "rightPaperHand.jpg";
        break;
        case 2:
            img.src = "rightScissorHand.jpg";
        break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM