1

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

<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 解决方案。

4

3 回答 3

3

基本上,您需要停止事件的传播,使用 event.cancelBubble(对于 IE)或 event.stopPropagation()(对于其他所有人)。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>

(当然,你可以把这一切都内联,但那是一团糟。)

您可能还会发现这个问题很有启发性。

于 2009-05-04T15:43:40.787 回答
0

stopPropogation 是您想要的。

http://docs.jquery.com/Events/jQuery.Event#event.stopPropagation.28.29

于 2009-05-04T15:44:09.803 回答
0

你可以写

onclick="false"

并使用事件模型级别 2 添加事件(ie 中的attachEvent 或其他浏览器中的 addEventListener)。如果你使用 jquery,你也可以 jquery 绑定('click')。

于 2009-05-04T15:50:01.863 回答