如何停止运行下一个功能?

时间:2012-05-11 09:51:56

标签: javascript events

我点击了顶部<tr>,然后点击tr > td > button时发生了一个事件。当我点击tr > td > button时,相关的onclick()函数正在运行,但<tr>上的相关函数也在之前运行。

像:

<tr onclick="run_tr();">
    <td><input type="button" onclick="run_td();" /></td>
</tr>

如何通过点击按钮停止自动拨打下一个电话?

3 个答案:

答案 0 :(得分:2)

在按钮onclick功能中,您需要停止事件冒泡:

event.stopPropagation();

关注您的代码:

HTML:

<input type="button" onclick="run_td( event );" />

JS:

function run_td( event ) {
    if ( event.stopPropagation ) { 
        event.stopPropagation();
    }
    event.cancelBubble = true;
    // ... rest of your code.
}

答案 1 :(得分:1)

您必须使用e.stopPropagation()方法停止事件的传播。有关详细信息,请查看以下链接:http://www.quirksmode.org/js/events_order.html#link9

答案 2 :(得分:0)

我不明白为什么你不能使用推荐的方法,这是一个例子:

<div onclick="alert('div click called');">
  Click here to see div onclick
  <button onclick="
    (event.stopPropagation && event.stopPropagation());
    event.cancelBubble = true;
    alert('stopped click');
  ">Button click but no div click</button>
</div>