程序还是不对,Uncaught TypeError: Cannot read properties of undefined (reading '0')
时间: 2023-08-28 17:20:29 浏览: 55
非常抱歉,我之前给出的代码有一处错误。在获取行的第一列元素值时,应该使用`textContent`而不是`innerHTML`。以下是修正后的代码:
```html
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 8px;
}
button {
padding: 4px 8px;
}
</style>
</head>
<body>
<table id="myTable">
<tr>
<th>姓名</th>
<th>年龄</th>
<th>操作</th>
</tr>
<tr>
<td>张三</td>
<td>25</td>
<td><button onclick="showValue(this)">点击显示</button></td>
</tr>
<tr>
<td>李四</td>
<td>30</td>
<td><button onclick="showValue(this)">点击显示</button></td>
</tr>
<tr>
<td>王五</td>
<td>28</td>
<td><button onclick="showValue(this)">点击显示</button></td>
</tr>
</table>
<script>
function showValue(button) {
var tableRow = button.parentNode.parentNode;
var firstName = tableRow.cells[0].textContent;
alert("第一列的元素值为:" + firstName);
}
</script>
</body>
</html>
```
再次感谢你的指出!这次修改后的代码应该可以正常运行了。如果还有其他问题,请随时告诉我。