一个表中的两个onclick事件


Two onclick events in a table

我有一个像这样的tr样式的表:

echo "<tr style='"background-color: $farbe;'" onmouseover='"this.style.cursor='pointer';this.style.backgroundColor='#87CEFF';'"onclick='"window.location='overview.php?adrnr=" . $inhalt["AdrNr"] . "''"  onMouseOut='"this.style.backgroundColor='$farbe';'">'n";

,我想在其中一个元素中有一个按钮,看起来像这样

<td> <input type = "button" value = "Bearbeiten" onClick="self.location.href='  changeadress.php'" >

问题是,只要我按下按钮,我就会得到overview。php因为td得到了有没有别的办法呢

您可以在IE中使用event. stoppropagation()或window.event.cancelBubble。

你可以像这样将它们组合在一个函数中:

<script type='text/javascript'>
    function stopEventBubble(event)
    {
       if (event.stopPropagation){
           event.stopPropagation();
       }
       else if(window.event){
          window.event.cancelBubble=true;
       }
    }
</script>
<table>
    <tr onclick="window.location='http://www.google.com'">
        <td>
            <button onclick="self.location.href='http://www.yahoo.com'; stopEventBubble(event)">stuff</button>
        </td>
    </tr>
</table>

主要采取从这个SO:如何停止事件传播与内联onclick属性?这里有一些很好的评论