简体   繁体   English

如何构造if ... else语句以返回true

[英]How to structure if…else statement to return true

Full code https://codepen.io/3noki/pen/xjyErQ 完整代码https://codepen.io/3noki/pen/xjyErQ

function checkForMatch() {
  if ( $(openedCardsList[0]).is(openedCardsList[1]) ) {
  $(openedCardsList[0]).removeClass('open show').addClass('match');
  $(openedCardsList[1]).toggleClass('open show');
  $(openedCardsList[1]).toggleClass('match');
  $(openedCardsList) = [];
  }

  else {unFlip()}
}

This code currently only returns false values, I would like to know how to structure this in either pure DOM or pure jquery format to be able to return a true value. 该代码当前仅返回假值,我想知道如何以纯DOM或纯jquery格式构造此结构,以便能够返回真实值。

openedCardsList[0]===openedCardsList[1]

Has not returned true either 也没有返回true

When the card at array 0 and 1 are the same I want this value to be returned true. 当数组0和1处的卡相同时,我希望此值返回true。

How about checking/matching against the (FontAwesome) icon class names? 如何检查/匹配(FontAwesome)图标class名称?

So when creating the card (or generating its HTML), you'd add fa to the element's "data" (using jQuery.data() ), like this: 因此,在创建卡片(或生成其HTML)时,您可以将fa添加到元素的“数据”中(使用jQuery.data() ),如下所示:

function createCardHTML() {
  symbol = cards.children("i");
  symbol.each(function(index, item) {
    $(item).addClass(cardsList[index])
      // If the icon's class name is `fa-diamond`, `$(item).data('fa')` would be `diamond`.
      .data('fa', cardsList[index].substring(3)); // <- here's the data
  });
  return symbol;
}

And then, in checkForMatch() : 然后,在checkForMatch()

var b = $('i.fa', openedCardsList[0]).data('fa');
var c = $('i.fa', openedCardsList[1]).data('fa');
var eq = ( b && c && b === c );

Demo 演示

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

相关问题 如何在if语句所在的for循环之外返回if语句的else - How to return else of an if statement outside the for loop the if statement is in 为什么这个函数只返回真,因为没有 else 语句使它在非真条件下返回假? - Why does this function return only true, since there is no else statement to make it return false under a non true condition? 如何在 IF 和 ELSE 都为真的 Blogger 中的 if 语句中中断语句? - How to break statement in a if statement in Blogger where both IF and ELSE are true? else return 语句隐藏 else if return 语句 - else return statement is shadowing else if return statement JavaScript短路if语句:如何返回true - Javascript short circuit if statement: How to return true 如果条件为真,如何使用if else语句清除JavaScript超时 - How to clear JavaScript timeout using the if else statement if condition is true 一旦条件为真,如何在 JavaScript 中中断 if/else 语句 - How to break if/else statement in JavaScript once a condition is true 仅当我的 if/else 语句为真时,如何才能显示图像? - How to get image to appear only if my if/else statement is true? 如果else语句内部返回“”语句 - If else statement inside return “ ” statement 如何在不使用if else语句的情况下最小化代码结构 - How to minify code structure without using if else statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM