我需要帮助修复我的移动代码。我不是那么友好的javascript。我只是使用旧的时尚表技术和JavaScript。我不能使用任何jquery或其他来源。这是代码:
<html>
<head>
<style>
table, td {
border: solid black;
text-align: center;
background-color: #DCDCDC;
}
td {
width: 20px;
}
</style>
<script>
var bposr =3; // blank position : row
var bposc=3; // blank position : column
function keypress() {
var ieKey = event.keyCode;
if (ieKey == 40 && bposr!=0){ // for moving down
temp= document.getElementById("myTable").rows[bposr-1].cells[bposc].innerHTML;
document.getElementById("myTable").rows[bposr-1].cells[bposc].innerHTML=document.getElementById("myTable").rows[bposr].cells[bposc].innerHTML;
document.getElementById("myTable").rows[bposr].cells[bposc].innerHTML=temp;
document.getElementById("myTable").rows[bposr].cells[bposc].style.backgroundColor='#DCDCDC';
document.getElementById("myTable").rows[bposr-1].cells[bposc].style.backgroundColor='#ffffff';
bposr--;
}
else if (ieKey == 37 && bposc!=1){ // for moving right
temp= document.getElementById("myTable").rows[bposr].cells[bposc+1].innerHTML;
document.getElementById("myTable").rows[bposr].cells[bposc+1].innerHTML=document.getElementById("myTable").rows[bposr].cells[bposc].innerHTML;
document.getElementById("myTable").rows[bposr].cells[bposc].innerHTML=temp;
document.getElementById("myTable").rows[bposr].cells[bposc].style.backgroundColor='#DCDCDC';
document.getElementById("myTable").rows[bposr].cells[bposc+1].style.backgroundColor='#ffffff';
bposc++;
}
else if (ieKey == 39 && bposc!=1){ // for moving left
temp= document.getElementById("myTable").rows[bposr].cells[bposc+1].innerHTML;
document.getElementById("myTable").rows[bposr].cells[bposc+1].innerHTML=document.getElementById("myTable").rows[bposr].cells[bposc].innerHTML;
document.getElementById("myTable").rows[bposr].cells[bposc].innerHTML=temp;
document.getElementById("myTable").rows[bposr].cells[bposc].style.backgroundColor='#DCDCDC';
document.getElementById("myTable").rows[bposr].cells[bposc+1].style.backgroundColor='#ffffff';
bposc++;
}
else if (ieKey == 38 && bposr!=0){ // for moving down
temp= document.getElementById("myTable").rows[bposr-1].cells[bposc].innerHTML;
document.getElementById("myTable").rows[bposr-1].cells[bposc].innerHTML=document.getElementById("myTable").rows[bposr].cells[bposc].innerHTML;
document.getElementById("myTable").rows[bposr].cells[bposc].innerHTML=temp;
document.getElementById("myTable").rows[bposr].cells[bposc].style.backgroundColor='#DCDCDC';
document.getElementById("myTable").rows[bposr-1].cells[bposc].style.backgroundColor='#ffffff';
bposr--;
}
}
function start(){
document.onkeydown = keypress;
}
</script>
</head>
<body onLoad="start()">
<center>
<table id="myTable">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
</tr>
<tr>
<td>13</td>
<td>14</td>
<td>15</td>
<td style="background-color:white;"></td>
</tr>
</table>
<br>
</body>
</html>
答案 0 :(得分:1)
在运动条件中你有一些逻辑错误,除了你的代码工作得很好。
var Table = document.getElementById("myTable");
var bposr =3; // blank position : row
var bposc=3; // blank position : column
function keypress(ieKey) {
var ieKey = event.keyCode;
var Table = document.getElementById("myTable");
console.log(Table);
console.log(ieKey);
if (ieKey == 40 && bposr != 0){ // for moving down
console.log(bposr);
temp = Table.rows[bposr - 1].cells[bposc].innerHTML;
Table.rows[bposr - 1].cells[bposc].innerHTML = Table.rows[bposr].cells[bposc].innerHTML;
Table.rows[bposr].cells[bposc].innerHTML = temp;
Table.rows[bposr].cells[bposc].style.backgroundColor = '#DCDCDC';
Table.rows[bposr - 1].cells[bposc].style.backgroundColor = '#ffffff';
bposr--;
} else if (ieKey == 39 && bposc != 0){ // for moving right
console.log(bposc);
temp = Table.rows[bposr].cells[bposc - 1].innerHTML;
Table.rows[bposr].cells[bposc - 1].innerHTML = Table.rows[bposr].cells[bposc].innerHTML;
Table.rows[bposr].cells[bposc].innerHTML = temp;
Table.rows[bposr].cells[bposc].style.backgroundColor = '#DCDCDC';
Table.rows[bposr].cells[bposc - 1].style.backgroundColor = '#ffffff';
bposc--;
} else if (ieKey == 37 && bposc != 3){ // for moving left
console.log(bposc);
temp = Table.rows[bposr].cells[bposc + 1].innerHTML;
Table.rows[bposr].cells[bposc + 1].innerHTML = Table.rows[bposr].cells[bposc].innerHTML;
Table.rows[bposr].cells[bposc].innerHTML = temp;
Table.rows[bposr].cells[bposc].style.backgroundColor = '#DCDCDC';
Table.rows[bposr].cells[bposc + 1].style.backgroundColor = '#ffffff';
bposc++;
} else if (ieKey == 38 && bposr != 3){ // for moving up
console.log(bposr);
temp = Table.rows[bposr + 1].cells[bposc].innerHTML;
Table.rows[bposr + 1].cells[bposc].innerHTML = Table.rows[bposr].cells[bposc].innerHTML;
Table.rows[bposr].cells[bposc].innerHTML = temp;
Table.rows[bposr].cells[bposc].style.backgroundColor = '#DCDCDC';
Table.rows[bposr + 1].cells[bposc].style.backgroundColor = '#ffffff';
bposr++;
}
}
function start(){
document.onkeydown = keypress;
}
&#13;
table, td {
border: solid black;
text-align: center;
background-color: #DCDCDC;
}
td {
width: 20px;
}
&#13;
<center>
<table id="myTable">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
</tr>
<tr>
<td>13</td>
<td>14</td>
<td>15</td>
<td style="background-color:white;"></td>
</tr>
</table>
<br>
</center>
&#13;
答案 1 :(得分:0)
感谢随机代码。我会在完成之前看看它是否可以打败。