gpt4 book ai didi

javascript - 从行点击获取点击列索引

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:58:28 26 4
gpt4 key购买 nike

我有一个行点击事件。最近不得不在每一行中添加一个复选框。如何识别行点击事件中被点击的单元格?

或者在单击复选框时阻止行单击。

尝试:this.parentNode.cellIndex 在行点击事件上未定义。

function pop(row){
alert(row.cells[1].innerText);
}
<table style="width:100%">
<tr>
<th>Select</th>
<th>Site</th>
</tr>
<tr onclick="pop(this);">
<td><input type="checkbox" id="123456" /></td>
<td>Lonodn</td>
</tr>
</table>

最佳答案

你想要这样的东西吗?您可以只检查事件源元素的 type 属性并验证是否允许它,您可以使用 e.stopPropagation();return;.

function pop(e, row) {
console.log(e.srcElement.type);
if(e.srcElement.type === 'checkbox'){
e.stopPropagation();
return;
}else{
console.log(row);
alert(row.cells[1].innerText);
}
}
<table style="width:100%">
<tr>
<th>Select</th>
<th>Site</th>
</tr>
<tr onclick="pop(event, this);">
<td><input type="checkbox" id="123456" /></td>
<td>Lonodn</td>
</tr>
</table>

关于javascript - 从行点击获取点击列索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46809391/

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