.mousedown()仅为每个元素调用一次

时间:2014-11-12 02:35:40

标签: javascript jquery html chess

我正在尝试下棋。到目前为止,我只使白色的棋子可以移动(你可以通过将它们拖到你想要的地方来移动它们)。但是,只有第一步才有效。为什么会这样?我用

$(".piece").mousedown(function(){}

,但只调用一次。

2 个答案:

答案 0 :(得分:1)

问题是$("#" + tileFn).append($("#" + tileIn).html());,它创建了一个新的piece元素,未附加mousedown处理程序。

一种解决方案是使用事件去除标记,或者不是创建新元素,只需移动现有元素,如

function parent(element) {
  var parentID = $(element).parent().attr("ID");
  var parentClass = $(element).parent().attr("class");
  var parentType = $(element).parent().get(0).nodeName;

  if (parentID != null) {
    return ("#" + parentID);
  } else if (parentClass != null) {
    return ("." + parentClass);
  } else {
    if (parentType.toLowerCase() == "body") {
      parentType = document;
      return parentType;
    } else {
      return parentType;
    }
  }
}
var dimensions = 600; // must be divisible by 8
var tile1 = "<div class='tile tile1' id='";
var tile2 = "<div class='tile tile2' id='";
var end = "'></div>";
var multiplicity = "";
var tileIn = "";
var tileFi = "";
var classes = "";
var color = "";
var type = "";
var possible = [];
$(document).ready(function() {
  //setup start
  for (var i = 0; i < 4; i++) {
    for (var j = 0; j < 4; j++) {
      row = i * 2 + 1;
      column = j * 2 + 1;
      $("#container").append(tile1 + row + column + end + tile2 + row + (column + 1) + end);
    }
    for (var k = 0; k < 4; k++) {
      row = i * 2 + 2;
      column = k * 2 + 1;
      $("#container").append(tile2 + row + column + end + tile1 + row + (column + 1) + end);
    }
  }
  $("#container").css({
    height: dimensions,
    width: dimensions
  });
  $(".tile").css({
    height: dimensions / 8,
    width: dimensions / 8
  });
  $(".piece").css({
    height: dimensions / 8,
    width: dimensions / 8
  });
  $("<div class='b p piece'><img src='bp.icns' height='69'></div>").appendTo("#21, #22, #23, #24, #25, #26, #27, #28");
  $("<div class='b r piece'><img src='br.icns' height='69'></div>").appendTo("#11, #18");
  $("<div class='b n piece'><img src='bn.icns' height='69'></div>").appendTo("#12, #17");
  $("<div class='b b piece'><img src='bb.icns' height='69'></div>").appendTo("#13, #16");
  $("<div class='b k piece'><img src='bk.icns' height='69'></div>").appendTo("#14");
  $("<div class='b q piece'><img src='bq.icns' height='69'></div>").appendTo("#15");

  $("<div class='w p piece'><img src='wp.icns' height='69'></div>").appendTo("#71, #72, #73, #74, #75, #76, #77, #78");
  $("<div class='w r piece'><img src='wr.icns' height='69'></div>").appendTo("#81, #88");
  $("<div class='w n piece'><img src='wn.icns' height='69'></div>").appendTo("#82, #87");
  $("<div class='w b piece'><img src='wb.icns' height='69'></div>").appendTo("#83, #86");
  $("<div class='w q piece'><img src='wq.icns' height='69'></div>").appendTo("#84");
  $("<div class='w k piece'><img src='wk.icns' height='69'></div>").appendTo("#85");
  //setup end
  $(".piece").mousedown(function() {
    tileIn = parent($(this)).substr(1, 2);
    classes = $(this).attr("class");
    color = classes.charAt(0);
    type = classes.charAt(2);
    y = tileIn.charAt(0);
    x = tileIn.charAt(1);
    //white start
    if (color == "w") {
      //white pawn start
      if (type == "p") {
        if (y == "7") {
          possible = ["6" + x, "5" + x];
        } else {
          possible = [(y - 1) + x];
        }
        return;
      }
      //white pawn end
      //
      else if ("a" == "b") {

      }
    }
    //white end
    //black start
    else {

    }
    //white end	
  });
  $(".tile").mouseup(function() {
    tileFn = $(this).attr("id");
    if (jQuery.inArray(tileFn, possible) !== -1 && $(this).is(':empty')) {
      $("#" + tileFn).append($("#" + tileIn).contents());
    } else {}
    possible = [];
  });
});
* {
  user-drag: none;
  -moz-user-select: none;
  -webkit-user-drag: none;
}
#container {
  margin: 0 auto;
  border: 1px solid gray;
}
.tile {
  float: left;
}
.tile1 {
  background-color: white;
}
.tile2 {
  background-color: rgba(0, 0, 0, 0.58);
}
.piece {
  padding: 3px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css">
<div id="container"></div>

答案 1 :(得分:0)

选择器$(".piece")仅在您执行语句时选择其上有pices的字段。当棋子在棋盘上移动时,你必须将这个功能添加到字段中。

因此,mouseup函数应该可以为新字段上的片段设置回调。