在html Javascript表中的单元格之间交换值
Swapping values between cells in html Javascript tables
我写了一个代码,它有一个4x4表,每个单元格中有数字1-15,一个单元格中有"空"。
当我单击任何相邻的单元格(上、左、右或下)为"空"时,它会交换这两个值。它似乎在中间两行工作得很好,但在顶部和底部一行却不行。有人能指出问题吗?
<script type="text/javascript">
var test = 0;
var startNumber = 0;
var arrayNumbers = new Array()
for (i = 0; i < 16; ++i) {
if (i == 15) { arrayNumbers[i] = "empty"; }
else {
++startNumber;
arrayNumbers[i] = startNumber
}
}
shuffle(arrayNumbers);
document.write("<table id = '"table1'" border = '"1'">")
for (k = 0; k < 4; ++k) {
document.write("<tr>")
for (i = 0; i < 4; ++i) {
document.write("<td>" + arrayNumbers[test] + "</td>");
++test
}
document.write("</tr>")
}
document.write("</table>")
// Table Made
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
var table = document.getElementById('table1'),
cells = table.getElementsByTagName('td');
var i = 0, len = cells.length
for (var i = 0, len = cells.length; i < len; i++) {
cells[i].onclick = function (index) {
return function () {
if (cells[index - 1].innerHTML == "empty" && index !== 4 && index !== 8 && index !== 12) {
cells[index - 1].innerHTML = cells[index].innerHTML;
cells[index].innerHTML = "empty";
}
else if (cells[index + 1].innerHTML == "empty" && index !== 3 && index !== 7 && index !== 11) {
cells[index + 1].innerHTML = cells[index].innerHTML;
cells[index].innerHTML = "empty";
}
if (cells[index - 4].innerHTML == "empty") {
cells[index - 4].innerHTML = cells[index].innerHTML;
cells[index].innerHTML = "empty";
}
if (cells[index + 4].innerHTML == "empty") {
cells[index + 4].innerHTML = cells[index].innerHTML;
cells[index].innerHTML = "empty";
}
}
}
(i)
}
</script>
在我看来,问题是检查数组中不存在的元素,从而引发错误。即当有人点击框3时,您将使用您的方法index-4=cells[-1]
进行调用。我怀疑你的所有优势都容易出错。
在每个循环中,您也可以简单地检查边界条件:
return function () {
if ((index-1 >= 0)
&& cells[index - 1].innerHTML == "empty"
&& index !== 4
&& index !== 8
&& index !== 12) {
cells[index - 1].innerHTML = cells[index].innerHTML;
cells[index].innerHTML = "empty";
}
else if ((index+1<=15)
&& cells[index + 1].innerHTML == "empty"
&& index !== 3
&& index !== 7
&& index !== 11) {
cells[index + 1].innerHTML = cells[index].innerHTML;
cells[index].innerHTML = "empty";
}
...
}
对你的+/-4也要这样做。这至少会让你更接近。此外,我建议安装一些可以用来调试javascript的东西(Firebug非常棒),因为这个错误很快就会显现出来。
相关文章:
- 使用jquery在单击时在单元格中输入值
- CSS-若窗口太小,滚动条会出现在“表格”单元格上
- 可以'我不明白为什么;t将行和单元格添加到表中
- 如何在Angular中的表的所有单元格中添加链接
- 表中单元格的总和
- ExtJS网格单元格编辑器,防止焦点松动问题
- 有没有一种方法可以仅使用HTML/CSS来隐藏基于特定值的数据单元格
- 区分电子表格中的空单元格和0值
- 返回两个日期时间单元格之间的差值
- jQuery - 删除表格行(如果任何单元格包含介于 X 和 Y 之间的值)
- 如何在当前行中突出显示拖动开始和拖动结束之间的所有单元格,拖动只能是当前选择行
- 使用 Javascript 显示 HTML 表中两个单元格之间的差异
- 在html Javascript表中的单元格之间交换值
- 在表格单元格中的值之间旋转
- 绘制两个位置之间的单元格
- 拖放动态生成的两个表之间的所有单元格
- 在行/单元格之间调用表的Onmouseout函数
- 移除网格单元格之间的空间
- Google Apps脚本,用于发现Google工作表的两列之间的差异,忽略空白单元格和多个实例
- Html5在表格单元格之间和单元格内拖动项目