<tr onclick="goToLink('/show_tui?tuihuodanhao={{item[1]}}');"> <td onclick=""><input type="checkbox" value="{{item[1]}}" name="id"></td>这样还是会触发onclick事件怎么办?
时间: 2023-07-02 11:16:56 浏览: 34
如果你想要在第二列中的复选框上点击时不触发整行的点击事件,可以在第二列的单元格中的HTML元素(即复选框)上添加一个onclick事件,并在该事件中调用事件对象的stopPropagation()方法,以阻止事件冒泡到父元素,如下所示:
```
<tr onclick="goToLink('/show_tui?tuihuodanhao={{item[1]}}');">
<td><input type="checkbox" value="{{item[1]}}" name="id" onclick="event.stopPropagation();"></td>
<td><a href="/show_tui?tuihuodanhao={{item[1]}}">其他列</a></td>
<td>其他列</td>
...
</tr>
```
在上面的代码中,第二列的单元格中的复选框添加了一个onclick事件,并在该事件中调用了stopPropagation()方法,以阻止事件冒泡到父元素(即整行的onclick事件),这样在第二列中的复选框上点击时,将不会触发整行的点击事件,只会触发复选框的点击事件。
相关问题
<tr onclick="goToLink('/show_tui?tuihuodanhao={{item[1]}}');"> <td onclick=""><input type="checkbox" value="{{item[1]}}" name="id"></td>这样还是会触发onclick事件怎么办
如果你想阻止点击事件的触发,可以在onclick事件中返回false或者调用事件对象的preventDefault()方法来阻止默认行为。你可以尝试将onclick事件改为以下代码:
```
<tr onclick="goToLink('/show_tui?tuihuodanhao={{item[1]}}'); return false;">
<td onclick=""><input type="checkbox" value="{{item[1]}}" name="id"></td>
</tr>
```
或者:
```
<tr onclick="event.preventDefault(); goToLink('/show_tui?tuihuodanhao={{item[1]}}');">
<td onclick=""><input type="checkbox" value="{{item[1]}}" name="id"></td>
</tr>
```
这样就可以阻止点击事件的默认行为了。
<tr onclick="goToLink('/show_tui?tuihuodanhao={{item[1]}}');"> <td id="cell1"><input type="checkbox" value="{{item[1]}}" name="id"></td> <script> function goToLink(link) { window.location = link; } </script> <script> var cell = document.getElementById("cell1"); cell.onclick = null; </script>错在哪里
这段代码存在一个逻辑错误,即在点击表格中的某一行时,会先执行 `goToLink` 函数跳转到对应的页面,然后再执行 `cell.onclick = null` 将该行的点击事件清空。这会导致无法再次点击该行,因为点击事件已经被清空了。正确的方式是将 `cell.onclick = null` 放在 `goToLink` 函数里面,即在跳转页面之前先清空点击事件,这样就可以正常点击了。修改后的代码如下所示:
```
<script>
function goToLink(link) {
var cell = document.getElementById("cell1");
cell.onclick = null;
window.location = link;
}
</script>
<table class="tab_1">
<tr>
<td><input type="checkbox" id="select-all"></td>
<td>录入时间</td>
<td>退货单号</td>
<td>录入人</td>
<td>备注</td>
<td>状态</td>
<td>日志</td>
</tr>
{%for item in data%}
<tr onclick="goToLink('/show_tui?tuihuodanhao={{item[1]}}');">
<td id="cell1"><input type="checkbox" value="{{item[1]}}" name="id"></td>
<td>{{item[0]}}</td>
<td><a href="/do_chawuliu2?danhao={{item[1]}}">{{item[1]}}</a></td>
<td>{{item[2]}}</td>
<td>{{item[3]}}</td>
<td>{% if item[4] is none %}{% else %}{{item[4]}}{% endif %}</td>
<td>日志</td>
</tr>
{%endfor%}
</table>
```