gpt4 book ai didi

javascript - jquery,防止父元素上的 onclick=window.location

转载 作者:行者123 更新时间:2023-11-30 13:43:40 26 4
gpt4 key购买 nike

我有一个包含如下数据的表格:

<table>
<tr onclick="window.location='download.php?id=1'">
<td>Program 1<br/>Short Description 1 (<a onclick="$.popup.show('Install Instructions', 'Instructions pulled from DB here')">Instructions</a>)</td>
<td>Added by user and date/time here<td>
<td>Filesize here<td>
<td><a href="edit.php?id=1">Edit</a> - <a href="delete.php?id=1">Delete</a><td>
</tr>
(repeat TRs for more programs pulled from DB)
</table>

我遇到的问题是,当您单击(说明)链接时,它应该会弹出一个包含说明的窗口。它确实做到了,但它也会触发 TR 的 onclick。所以我在一瞬间看到了弹出窗口,然后它转到了 download.php。

有什么方法可以阻止它触发 TR 的 onclick 吗?最好使用 jquery,但我也不介意普通的 javascript 解决方案。

最佳答案

基本上,您需要停止 propagation of events ,使用 event.cancelBubble(对于 IE)或 event.stopPropagation()(对于其他人)。 solution given from quirksmode (我以前用过)是做这样的事情:

<script type="text/javascript">
var instructions = function(e) {
// Get the correct event in a cross-browser manner, then stop bubbling/propagation.
if (!e) var e = window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();

// Do what you want now
$.popup.show('Install Instructions', 'Instructions pulled from DB here');
}
</script>

然后你的脚本会调用它:

<td>Program 1<br/>Short Description 1 (<a onclick="instructions(event)">Instructions</a>)</td>

(当然,您可以将它们全部内联,但那真是一团糟。)

您还可以找到 this question很有启发性。

关于javascript - jquery,防止父元素上的 onclick=window.location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/820583/

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