Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将.each()与数组一起使用_Javascript_Jquery - Fatal编程技术网

Javascript 将.each()与数组一起使用

Javascript 将.each()与数组一起使用,javascript,jquery,Javascript,Jquery,我正在使用jQuery创建一个匹配的纸牌游戏。目前,我遇到了一个问题,代码中的playerChoices数组没有使用“matched”类更新“matched”卡 var playerChoices = []; function showCard(){ $(this).addClass('selected'); //mark the selection with the selected class playerChoices.push($(this)); //push th

我正在使用jQuery创建一个匹配的纸牌游戏。目前,我遇到了一个问题,代码中的playerChoices数组没有使用“matched”类更新“matched”卡

var playerChoices = [];

function showCard(){

    $(this).addClass('selected'); //mark the selection with the selected class

    playerChoices.push($(this)); //push the players choice onto the playerChoices array
    console.log(playerChoices);

    moves++;
    console.log(moves);

    $('#buttons').show();
    matchCards(playerChoices);

}
以下是问题所在的功能:

function matchCards(array){

    if(playerChoices.length === 3){
        //when the player finds the first match
        if(playerChoices[0].attr('class') === playerChoices[1].attr('class')){  //if both playerChoices have the class 
            console.log("match found at index 0 and 1 of playerchoice!");
            **$(playerChoices).each(playerChoices, function(index, element){
                $(this).addClass('matched');**
            })
        }

        //Deselect the two choices that were made
        else{ 
            $(playerChoices).each(function(index, element){
                $(this).removeClass('selected');
            })
            playerChoices = [];
        }
    }

    else if(playerChoices.length === 4){

        //when the player gets the second match
        if(playerChoices[2].attr('class') === playerChoices[3].attr('class')){
            console.log("match found at index 2 and 3 of playerchoice!");
            **$(playerChoices).each(playerChoices, function(index, element){
                $(this).addClass('matched');**
            })
            **showGameOverMessage();**
        }

        //Deselect the last two choices that were made
        else{
            $(playerChoices).each(function(index, element){
                $(this).removeClass('selected');
            })
        }
    }
}
这里的主要问题是我的代码中有“星号”的区域。我在控制台中设置了断点,发现代码从未到达$(this.addClass('matched')行。我以前从未使用过.each,也看过示例api.jquery.com,但我仍然无法克服将匹配类应用于“匹配”卡的问题

仅供参考:我试图让我的代码在JSFIDLE中工作,但我的卡的图像不断出错。我的代码在这之外工作,我只是无法让匹配的类适当地应用

现在工作


非常感谢您的帮助

您更新的问题清楚地说明了问题:您正在将jQuery实例推送到
playerChoices

playerChoices.push($(this));
…然后稍后使用
$(playerChoices)。每个(…)
尝试在其上循环。虽然
$()
$()
函数中接受HTML元素数组,但如果您向它传递一个jQuery实例数组,它就不能正确理解它-您最终会得到一个围绕该组jQuery实例的jQuery实例,这是没有用的-您也可以使用该数组(或者使用一个jQuery实例,我将在后面介绍)

您可以使用(在
jQuery
函数本身上的一个):

但是您确实不需要
$。每个
,只需使用阵列的内置
forEach

playerChoices.forEach(function(entry) {
    // ...`entry` here will be a jQuery instance
    entry.addClass('matched');
});

…或者有很多其他方法可以通过我的答案中列出的数组进行循环

<> > <强> >,您可以考虑制作<代码> PrReReals> <代码> >(单个)jQuery实例。jQuery是基于设置的,因此单个jQuery实例可以包含多个HTML元素,然后可以用一个方法调用来进行操作。例如,如果您制作了<代码> PrReReals> < /Cord>一个jQuery实例,而不是:

playerChoices.forEach(function(entry) {
    entry.addClass('matched');
});
您可以这样做:

playerChoices.addClass('matched');
为此,请从以下几点开始:

playerChoices = $();
…并通过
add
添加元素:

playerChoices.add(this);

在回调之前尝试删除
playerChoices

$(playerChoices).each(function(index, element){
  $(this).addClass('matched');
})

JSFIDLE

但是OP将数组包装到jQuery对象中,这应该是可以的:
$(playerChoices)
@dfsq:如果
playerChoices
中的条目是HTML元素实例,我无法从代码中判断它们是否是HTML元素实例(这就是问题的原因)。如果它们是HTML元素,这不是正确的答案。@TJCrowder问题似乎是
playerChoices
at
$(playerChoices)。每个(playerChoices,
之前callback@guest271314:为什么这会相关?@T.J.Crowder因为
.each()
在回调之前不需要变量在哪里向
playerChoices
添加条目?查看我的编辑,我添加了showCard()函数“我试图让我的代码在JSFiddle中工作”你能发布到JSFiddle的链接吗?什么是
$(这个)
应该在
中。each()
回调?$('.card')。在('click',showCard')上;它是一个保存卡本身的.card类。编辑:刚刚添加了一个JSFIDLE链接。我能够克服图像问题。但现在它抱怨jQuery脚本。@ziggy尝试显式使用
元素,并将
src
设置为
https:
协议。另请参阅plnkr以获得JSFIDLE的替代方案
$(playerChoices).each(function(index, element){
  $(this).addClass('matched');
})