2

I have an event on clicking the top <tr> and then also an event on clicking the tr > td > button. When i click the tr > td > button, the related onclick() function is running but, the function related on <tr> is also running after previous.

Like:

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

How can i stop automatically calling the next call by clicking the button?

4

3 回答 3

3

在按钮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.
}
于 2012-05-11T09:54:32.077 回答
1

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

于 2012-05-11T09:55:24.290 回答
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>
于 2012-05-11T10:37:58.810 回答