是否可以拥有<a> trigger before an onclick handler for a parent element?

时间:2018-04-24 16:00:36

标签: html dom-events

I have a table like this:

<table>
  <tr onclick="window.location='foo.html'">
    <td>Data 1</td>
    <td>Data 2</td>
    <td><a href="bar.html">link</a></td>
  </tr>
</table>

When I try to click the link text, I get sent to foo.html. I'd like to go to bar.html instead. Is there a way to make sure that the <a> gets the click event first?

Or do I have to change my onclick handler to be on each of the <td> elements except for the final one containing the link?

2 个答案:

答案 0 :(得分:2)

要获得预期结果,请使用以下z-index

选项
a{
  position:relative;
  z-index:9999
}

https://codepen.io/nagasai/pen/gzrzgO

答案 1 :(得分:1)

您可以使用onclick作为锚标记:

<a href="#" onclick="window.location='bar.html'">link</a>