简体   繁体   English

为什么我的JS代码的其余部分不让我的JQuery运行?

[英]Why is the rest of my JS code not letting my JQuery run?

apologies for the basic question but I'm just starting to learn JS. 为基本问题道歉,但我刚开始学习JS。 I'm trying to build a blackjack game, and I started off in my .js file by creating a constructor function (for the cards) and then I defined one of the methods in that function. 我正在尝试构建一个二十一点游戏,我开始在我的.js文件中创建一个构造函数(对于卡片),然后我定义了该函数中的一个方法。 Here is the code in my .js file (I've not shown what's inside the actual cardToString function as it's about 70 lines long): EDIT: Some people have asked I include the entire code, so here it is: 这是我的.js文件中的代码(我没有显示实际cardToString函数中的内容,因为它大约有70行):编辑:有些人问我包含了整个代码,所以这里是:

 function Card(rank, suit) {
this.rank = rank;
this.suit = suit;
this.toString = cardToString;
this.createNode = cardCreateNode;
}
function cardToString() {
var rank;
var suit;
switch (this.rank) {
    case "A" :
        rank = "Ace";
        break;
    case "2" :
        rank = "Two";
        break;
    case "3" :
        rank = "Three";
        break;
    case "4" :
        rank = "Four";
        break;
    case "5" :
        rank = "Five";
        break;
    case "6" :
        rank = "Six";
        break;
    case "7" :
        rank = "Seven";
        break;
    case "8" :
        rank = "Eight";
        break;
    case "9" :
        rank = "Nine";
        break;
    case "10" :
        rank = "Ten";
        break;
    case "J" :
        rank = "Jack";
        break;
    case "Q" :
        rank = "Queen";
        break;
    case "K" :
        rank = "King";
        break;
    default :
        rank = null;
        break;
    }
switch (this.suit) {
    case "C" :
        suit = "Clubs";
        break;
    case "D" :
        suit = "Diamonds";
        break;
    case "H" :
        suit = "Hearts";
        break;
    case "S" :
        suit = "Spades";
        break;
    default :
        suit = nill;
        break;
    }

if (rank == null || suit == null)
    return "";
else
    return rank + " of " suit;
}

$(document).ready(function() {
$("#deal").click(function() {
    $("#hit").fadeOut('slow');
});

});

I'm completely confused, because when I put this into the .js file the jQuery doesn't work, however when I comment out everything but the jQuery it works fine. 我完全感到困惑,因为当我把它放到.js文件中时,jQuery不起作用,但是当我注释掉除了jQuery之外的所有内容时它运行正常。 I'm sure it's something basic that I'm just not aware of, but I've searched for a while and can't find an answer. 我确定这是一些基本的东西,我只是不知道,但我已经搜索了一段时间,但找不到答案。 Basically, I have no idea why my first few bits of JS stop the jQuery from working. 基本上,我不知道为什么我的前几个JS会阻止jQuery工作。

EDIT: Someone asked me if I checked the console for errors, and sure enough I do get one: 编辑:有人问我是否检查了控制台的错误,我确实得到了一个:

Uncaught SyntaxError: Unexpected identifier Uncaught SyntaxError:意外的标识符

Next to this it said "jsjquery.js:4" and I think 4 refers to the line of code which was an empty line right at the beginning (I have some comments on the first few lines, then left a line before I starting coding). 接下来它说“jsjquery.js:4”,我认为4指的是代码行,它在开头就是一个空行(我在前几行有一些注释,然后在开始编码之前留下一行)。 I removed empty line, now it says the error is on line 80, which is this piece of code: 我删除了空行,现在它说错误是在第80行,这是这段代码:

 return rank + " of " suit;

This error goes away when I comment out everything but the jQuery. 当我评论除了jQuery之外的所有东西时,这个错误消失了。 This might make everything clear to you guys, but I'm still lost! 这可能会让一切都清楚,但我还是迷路了!

Thanks. 谢谢。

EDIT: Ok final edit I promise, haha. 编辑:好的最终编辑我保证,哈哈。 For anyone who might come across this question looking for an answer: I made a couple of syntax mistakes, but the real problem was that I didn't define the method cardCreateNode. 对于任何可能遇到这个问题寻找答案的人:我犯了几个语法错误,但真正的问题是我没有定义方法cardCreateNode。 I didn't realise that not doing so would cause everything else to not run. 我没有意识到不这样做会导致其他一切都不能运行。 Thank you to everyone who responded! 谢谢大家的回复!

default :
        suit = nill;
        break;

unexpected identifier is syntax error. 意外标识符是语法错误。 Obviously you mean null here, not nill. 显然你的意思是null,而不是nill。

Since you have a different error based on the update of your post I'll answer your latest issue being that you receive an error on this line: 由于您根据帖子的更新有不同的错误,我将回答您的最新一期,因为您在此行收到错误:

return rank + " of " suit;

You are missing the concatenation on the other side of " of " so it should look like this: 你错过了“of”的另一面的连接,所以它应该是这样的:

return rank + " of " + suit;

Also in your switch statement you have this line: 同样在你的switch语句中你有这一行:

default :
    suit = nill;
    break;

I think here you mean to set the value to null not nill which would look like this: 我想在这里你的意思是将值设置为null而不是nill,如下所示:

default :
    suit = null;
    break;

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

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