我是jQuery编程的新手,我正在尝试制作国际象棋游戏。我只是想请你帮忙,当我拖动它们时如何让我的碎片移动。这是我的代码的一部分。您还可以在我的小提琴代码中看到其他部分(发布以下链接):
<table id="chess_board" cellpadding="0" cellspacing="0">
<tr>
<td id="A8"><a href="#" class="rookBlack">♜</a></td>
<td id="B8"><a href="#" class="nightBlack">♞</a></td>
<td id="C8"><a href="#" class="bishopBlack">♝</a></td>
<td id="D8"><a href="#" class="kingBlack">♛</a></td>
<td id="E8"><a href="#" class="queenBlack">♚</a></td>
<td id="F8"><a href="#" class="bishopBlack">♝</a></td>
<td id="G8"><a href="#" class="nightBlack">♞</a></td>
<td id="H8"><a href="#" class="rookBlack">♜</a></td>
</tr>
<tr>
<td id="A7"><a href="#" class="pawnBlack">♟</a></td>
<td id="B7"><a href="#" class="pawnBlack">♟</a></td>
<td id="C7"><a href="#" class="pawnBlack">♟</a></td>
<td id="D7"><a href="#" class="pawnBlack">♟</a></td>
<td id="E7"><a href="#" class="pawnBlack">♟</a></td>
<td id="F7"><a href="#" class="pawnBlack">♟</a></td>
<td id="G7"><a href="#" class="pawnBlack">♟</a></td>
<td id="H7"><a href="#" class="pawnBlack">♟</a></td>
</tr>
答案 0 :(得分:0)
如果您只是通过在页面上引用来使用jQuery库,那么这很容易做到:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
然后在你的Javascript中只需要在你想要拖动的任何东西上调用.Draggable函数。很简单:
$("a").draggable(); // this would make any `a` tag draggable.
以下是jQuery中可拖动功能的链接:http://jqueryui.com/draggable/
另请参阅他们提供的可放置功能:http://jqueryui.com/droppable/
答案 1 :(得分:0)
添加此脚本
$(function() {
$( "a" ).draggable();
});
Here you can Downlaod the Jquery Draggable Code
在这里你去 Demo
<强> Here New answer 强>
Dude添加此代码
$('a').draggable({ containment: "table", revert: 'invalid' });
$('td').droppable({
drop: function(ev, ui) {
var dropped = ui.draggable;
var droppedOn = $(this);
$(droppedOn).droppable("disable");
$(dropped).parent().droppable("enable");
$(dropped).detach().css({top: 0, left: 0}).appendTo(droppedOn);
}
});
$('td').not('td:empty').droppable("disable");