gpt4 book ai didi

javascript - 显示使用 javascript 动态创建的表的 tr 和 td 索引?

转载 作者:太空宇宙 更新时间:2023-11-04 15:53:09 25 4
gpt4 key购买 nike

我创建了一个表/矩阵,使用 javascript 我想在单击表数据时打印表行和表数据的索引

<html>
<head>
<title>Table Creation</title>

<script language="javascript" type="text/javascript">
function createTable()
{
document.write("<table >");

for (var x = 1; x < 10; x++) {
document.write("<tr >");

for(var y = 1; y < 10; y++) {
document.write("<td><button>index</button></td>");
}

document.write("</tr>");
}

document.write("</table>");
}
</script>
</head>

<body>
<button onclick="createTable()">Try it</button>
<div >
</div>
</body>
</html>
</html>

例如,第 1 行警报应为 ((1,1),(1,2),(1,3)...,第 2 行警报应为 ((2,1),(2, 2),(2,3))

最佳答案

检查这个:

<html>
<head>
<title>Table Creation</title>

<script language="javascript" type="text/javascript">
function createTable()
{
document.write("<table >");
for (var x=1; x <10; x++) {
document.write("<tr >");
for(var y=1; y<10; y++) {

document.write("<td><button onclick='alertButton("+x+","+y+")'>index</button></td>");

}
document.write("</tr>");
}
document.write("</table>");
}
function alertButton(row, col){
alert('('+row+','+col+')');
}
</script>
</head>
<body>
<button onclick="createTable()">Try it</button>
<div >
</div>

</body>
</html>
</html>

关于javascript - 显示使用 javascript 动态创建的表的 tr 和 td 索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48320387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com