简体   繁体   English

尝试自己制作纸,石头,剪刀游戏

[英]Trying to make a Paper, Rock, Scissors game on my own

I am a javascript and jQuery beginner (as in barely a week in), and I need some experienced people to tell me what's wrong with this code. 我是javascript和jQuery的初学者(仅在不到一周的时间内),我需要一些有经验的人告诉我这段代码有什么问题。 Clicking a div does nothing. 单击div不会执行任何操作。 I could google for hours, but you guys could probably see the issue(s) way quicker, and I'll be able to learn from the mistake(s) and move on. 我可以在Google上搜索几个小时,但你们可能会更快地看到问题,并且我将能够从错误中学习并继续前进。 In my code, I replaced the very last command with "console.log" and copy/pasted everything into textedit to see if it would give me "answer" in the console when I clicked. 在我的代码中,我用“ console.log”替换了最后一个命令,并将所有内容复制/粘贴到textedit中,以查看单击该按钮是否会在控制台中给我“答案”。 But I got nothing. 但是我什么都没有。 Thank you guys ahead of time. 谢谢你们提前。

My code is here . 我的代码在这里

... and here ... 和这里

var box = "div";

$(document).ready(

function () {
    $(box).fadeTo(1000, 0.8);
});



$(box).click(

function () {
    var userchoice = this.id;
    var answer;
    var computerchoice = Math.floor(Math.random() * 3 + 1);

    if (computerchoice === 1) {
        computerchoice = "rock";
    }

    if (computerchoice === 2) {
        computerchoice = "paper";
    }

    if (computerchoice === 3) {
        computerchoice = "scissors";
    }

    if (userchoice === computerchoice) {
        answer = "It's a tie.";
    }
    if (userchoice === "rock") {
        if (computerchoice === "paper") {
            answer = "Computer Wins with " + computerchoice + ".";
        } else {
            answer = "You win with " + userchoice + ".";
        }
    }
    if (userchoice === "paper") {
        if (computerchoice === "scissors") {
            answer = "Computer Wins with " + computerchoice + ".";
        } else {
            answer = "You win with " + userchoice + ".";
        }
    }
    if (userchoice === "scissors") {
        if (computerchoice === "rock") {
            answer = "Computer Wins with " + computerchoice + ".";
        } else {
            answer = "You win with " + userchoice + ".";
        }
    }
    document.getElementById("header").innerHTML(answer);
});

If you check your browser's JS console you'll see this error: 如果您检查浏览器的JS控制台,则会看到此错误:

Uncaught TypeError: Property 'innerHTML' of object #<HTMLHeadingElement> is not a function 

This is telling you that .innerHTML is a property that you would assign a value to, not a function to be called, so you need to change this line: 这告诉您.innerHTML是您将为其分配值的属性,而不是要调用的函数,因此您需要更改此行:

document.getElementById("header").innerHTML(answer);

to: 至:

document.getElementById("header").innerHTML = answer;

Demo: http://jsfiddle.net/CUTez/15/ 演示: http//jsfiddle.net/CUTez/15/

But since you're using jQuery, you can do this: 但是由于您使用的是jQuery,因此可以执行以下操作:

$("#header").html(answer);

Note also that if you run the same code outside jsfiddle you will need to move the $(box).click(...) code inside the document ready handler, or put the whole script element at the end of your page just before the closing </body> tag, otherwise the $(box) part won't find any elements because they won't have been parsed yet. 还要注意,如果在jsfiddle外部运行相同的代码,则需要将$(box).click(...)代码移至文档就绪处理程序内,或者将整个脚本元素放在页面末尾,即关闭</body>标记,否则$(box)部分将找不到任何元素,因为它们尚未被解析。 (It works in the fiddle because by default jsfiddle puts all of the code in an onload handler.) (它在小提琴中起作用,因为默认情况下jsfiddle将所有代码放在onload处理程序中。)

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

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