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

.mousedown() only called once for each element

本文关键字:调用 一次 元素 mousedown      更新时间:2023-09-26

我正在尝试制作一个国际象棋游戏。到目前为止,我只让白色的棋子可以移动(你可以把它们拖到你想去的地方来移动它们)。然而,只有第一步有效。为什么会发生这种情况?我使用

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

,但只调用一次。

问题是$("#" + 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>

选择器$(".piece")只选择在执行语句时具有pice的字段。当工件在板上移动时,您必须将函数添加到字段中。

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