纸牌游戏拖放

时间:2016-06-12 12:43:29

标签: javascript jquery

  

有人可以玩这个游戏并向我解释为什么102行中的changelog类等于1?我在上面放了一个警告框来确认长度。   这是一个工作小提琴http://jsfiddle.net/6haMV/141/

A
.correct.length
$(init);

function init() {

  $('#element_1').data('number', 1).attr('id', 'card' + 1).draggable({
    containment: '#content',
    stack: '#cardPile div',
    cursor: 'pointer',
    revert: true

  });

  $('#element_2').data('number', 2).attr('id', 'card' + 2).draggable({
    containment: '#content',
    stack: '#cardPile div',
    cursor: 'pointer',
    revert: true
  });

  $('#element_3').data('number', 3).attr('id', 'card' + 3).draggable({
    containment: '#content',
    stack: '#cardPile div',
    cursor: 'pointer',
    revert: true
  });

  $('#element_4').data('number', 4).attr('id', 'card' + 4).draggable({
    containment: '#content',
    stack: '#cardPile div',
    cursor: 'pointer',
    revert: true
  });

  $('#element_5').data('number', 5).attr('id', 'card' + 5).draggable({
    containment: '#content',
    stack: '#cardPile div',
    cursor: 'pointer',
    revert: true
  });

  $('#element_6').data('number', 6).attr('id', 'card' + 6).draggable({
    containment: '#content',
    stack: '#cardPile div',
    cursor: 'pointer',
    revert: true
  });


  $('#slot_1').data('number', 1).droppable({
    accept: '#cardPile div',
    hoverClass: 'hovered',
    drop: handleCardDrop

  });

  $('#slot_2').data('number', 2).droppable({
    accept: '#cardPile div',
    hoverClass: 'hovered',
    drop: handleCardDrop
  });

  $('#slot_3').data('number', 3).droppable({
    accept: '#cardPile div',
    hoverClass: 'hovered',
    drop: handleCardDrop
  });

  $('#slot_4').data('number', 4).droppable({
    accept: '#cardPile div',
    hoverClass: 'hovered',
    drop: handleCardDrop
  });

  $('#slot_5').data('number', 5).droppable({
    accept: '#cardPile div',
    hoverClass: 'hovered',
    drop: handleCardDrop
  });

  $('#slot_6').data('number', 6).droppable({
    accept: '#cardPile div',
    hoverClass: 'hovered',
    drop: handleCardDrop
  });

}

var totalCards = $('#cardPile').children().length;
var totalMatches = 0;

function handleCardDrop(event, ui) {
  var slotNumber = $(this).data('number');
  var cardNumber = ui.draggable.data('number');

  if (slotNumber == cardNumber) {
    totalMatches++;
    ui.draggable.addClass('correct');
    ui.draggable.draggable('disable');
    alert("the .correct length is " + $(".correct").length);
    $(this).droppable('disable');
    ui.draggable.position({
      of: $(this),
      my: 'left top',
      at: 'left top'
    });
    ui.draggable.draggable('option', 'revert', false);
  }

  if (totalMatches == totalCards) {
    $('#basicModal').modal('show');
  }
}

1 个答案:

答案 0 :(得分:0)

代码 $(“。correct”)。length 告诉DOM上具有类选择器.correct的元素数。当你丢弃一个盒子然后在一个元素上存在类时,当你丢弃2然后元素数量增加到2,依此类推....