如何检查与单击的表单元格相邻的值,并在该值为“0”时替换该值;空”;
How to check for values adjacent to clicked table cell and replace that value if it is "empty"
到目前为止,我已经编写了一个代码(从其他站点提取的一些函数),它生成了一个包含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
document.write("askjd")
document.write(document.getElementById("table1").getElementsByTagName("tr")[2])
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');
for (var i = 0, len = cells.length; i < len; i++) {
cells[i].onclick = function () {
window.alert(this.innerHTML);
}
}
</script>
如果有人能帮忙,那就太好了!
我只需要从i = 1
循环到i < len - 1
,然后在cells[i]
、cells[i-1]
和cells[i+1]
上施展魔法。
也许您只想使用innerText来避免引用<>&'"
等问题。
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 对于如何在没有modernizr的情况下用边界半径的功能检测检查来替换我的旧浏览器检查
- 如何检查和替换数组中的值
- Ruby数组检查并替换为
- 检查是否在一定时间间隔内加载了多个图像;如果没有,则替换为其他 URL
- JavaScript 检查并替换两个字符串
- 如何检查与单击的表单元格相邻的值,并在该值为“0”时替换该值;空”;
- 如果捕获组等于replace()中的某个值,我该如何检查regex替换
- 在检查多个整数范围时替换多个if语句
- 如何检查字符串是否包含数组中的某个字符串,然后替换它
- 替换非字母数字字符以使用 javascript 检查回文
- JavaScript - 在对象数组中运行 forEach.想要检查字符串中的子字符串并将其替换为新的子字符串
- 如何检查类,然后替换每个元素的data-id
- 使用===来检查String.replace()是否实际执行了替换
- 检查内容等于什么,如果是某个字符串,则将其替换为unicode
- jQuery或Javascript或php脚本来检查每个元素并替换文本
- 对span元素执行JavaScript循环,检查它们的值并替换其他节点边框颜色
- 如何检查这些字符串中是否有任何一个存在于另一个字符串中,如果JavaScript中的答案是肯定的,则将其替换
- 当内容被Backbone.js替换时,如何解锁jquery ui检查按钮
- Javascript检查类是否存在,如果存在则替换为相同的类