简体   繁体   English

如何在javascript中相互比较两个列表项?

[英]How do I compare two list items against each other in javascript?

I'm working on a project with a memory game of cards that you try to guess by flipping two and see if they match. 我正在开发一个带有记忆卡游戏的项目,您可以尝试通过翻转两张来猜猜它们是否匹配。

I have a function that shuffles them, and then another that restricts that only 2 cards should be flipped one after the other (besides the already guessed ones) and now I'm trying to figure this out: 我有一个洗牌的功能,然后是另一个功能,它限制了只能翻转两张卡片(除了已经猜到的卡片之外),现在我想弄清楚这一点:

You flip one card (click event) and then you flip another, then I want javascript to compare them (they have fontawesome icons on them) and see if they are the same, if they are, I'll implement some animation but if not they flip back down and another animation goes off. 翻转一张卡片(单击事件),然后翻转另一张卡片,然后我想让javascript对其进行比较(它们上有fontawesome图标),看看它们是否相同,如果是,我将实现一些动画,但是如果没有他们向下翻转,另一个动画消失了。 Here's my HTML: 这是我的HTML:

<ul class="deck">
  <li class="card">
    <i class="fa fa-diamond"></i>
  </li>
  <li class="card">
    <i class="fa fa-paper-plane-o"></i>
  </li>
  (16 total list items, 8 of the same time just shuffled around randomly)
</ul>

I was thinking about adding another class of 1, 2, 3, etc. and matching the two similar ones like 1-1 2-2 then check if they match but my javascript skills are not good enough. 我正在考虑添加另一类1、2、3等,并匹配1-1 2-2这两个类似的类,然后检查它们是否匹配,但是我的JavaScript技能还不够好。

using jQuery, you can check if both cards have the same class, like this: 使用jQuery,您可以检查两个卡是否具有相同的类,如下所示:

if (firstCard.children().attr("class") == secondCard.children().attr("class")) {
// do the animation  
} else {
// do something else 
}

You need to use the children() selector of jQuery because the class that determines how your card looks like is a child of <li class="card"> . 您需要使用jQuery的children()选择器,因为确定卡外观的类是<li class="card">的子<li class="card">

EDIT: 编辑:

To get the two cards you want to compare, you could do this: 要获得要比较的两张卡,可以执行以下操作:

let openArray = []; 
$(".card").on("click", function() {
  openArray.push($(this)); 
});

and now when you have two cards in your array, perform the check, like this: 现在,当阵列中有两张卡时,执行检查,如下所示:

if(openArray.length == 2) {
  // perform the check
  let firstCard = openArray[0]; 
  let secondCard = openArray[1]; 

  if (firstCard.children().attr("class") == secondCard.children().attr("class")) {
    // do the animation  
  } else {
    // do something else 
 }
}

You can add a custom data attribute to each card to add for example id's for each. 您可以向每张卡添加自定义数据属性,例如为每张卡添加ID。 That way you don't rely on the applied classes. 这样,您就不必依赖于所应用的类。 And then compare the id's. 然后比较ID。

<li class="card" data-id="2">

Working fiddle here 在这里工作

Unless you are programmatically generating these cards at random, this seems like a trivial task. 除非您以编程方式随机生成这些卡,否则这似乎是一项琐碎的任务。

That aside, you should identify the parameters of this test. 除此之外,您应该确定此测试的参数。 Are there only ever 2 cards? 只有2张卡吗? Will the class names always occur in order of fa , followed by fa-icon-slug ? 将类名总是发生在顺序fa ,随后fa-icon-slug

The first question it seems like you've answered. 似乎您已经回答了第一个问题。 The second question is important because it tells us that the class names for each card element are sorted , which is very important for how we write our JavaScript. 第二个问题很重要,因为它告诉我们每个card元素的类名都是已排序的 ,这对于我们编写JavaScript的方式非常重要。

Let's assume, for the sake of argument, that your class names are sorted and that there will only ever be 2 class names on these card elements. 为了便于讨论,我们假设对您的类名进行了排序,并且这些卡元素上只会有2个类名。 That means we can just grab the second (or the last) class name for comparison. 这意味着我们只需要获取第二个(或最后一个)类名即可进行比较。

var $cards = $('.card'); // select all card elements

// get class value from each elem
// then split string into array delimited by space characters
// lastly, get the second value in the array (it's zero-indexed).
var cardOneValue = $cards[0].attr('class').split(' ')[1];
var cardTwoValue = $cards[1].attr('class').split(' ')[1];

if (cardOneValue == cardTwoValue) {
    // match
}

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

相关问题 jQuery / Javascript相互比较两个表 - jQuery/Javascript compare two tables against each other 如何比较两组1000个数字? - How can I compare two sets of 1000 numbers against each other? 如何比较数组中的每个数字? (JavaScript的) - How to compare every number in an array against each other? (javascript) 如何在 div 中显示 javascript 列表中的所有项目? - How do I display all the items from a javascript list under each other in a div? 如何使用JQuery相互检查两个input [text]值? - How do I check two input[text] values against each other, using JQuery? 如何将 JavaScript 日期与特定时区中的日期进行比较? - How do I compare a JavaScript date against a date in a specific timezone? 使用 Javascript,如何比较 2 个值以查看它们是否可以相互整除? - Using Javascript, how do I compare 2 values to see if they are divisible by each other? 如何使两个JavaScript数组的项目彼此对应? - How can I make items of two JavaScript arrays correspond to each other? 如何使用javascript保存类列表,以便以后与其他类进行比较? - How to save a list of classes using javascript to compare them against other classes later? 如何比较来自两个变量的值,这些变量在JavaScript中映射其他变量后得到了它们的值? - How do I compare values from two variables that got their values as result of mapping other variables in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM