How to ignore the first event and move on to the second?

I have a row table that has a Javascript action attached to mouse clicks anywhere inside the table row:

<tr onmousedown="someAction(this)">
    <td>cell 1</td>
    <td>cell 2</td>
    <td><a href="page2.html">cell 3</a></td>
</tr>

If the user clicks anywhere on the line, I need to execute a Javascript action, someAction (this). However, if the user clicks on the link to page2, then I want the action to be ignored and the link to follow.

Unfortunately, the action that takes place first is to move the linked text position enough to click it. In other words, the user cannot actually click the link and go to page2 in this case.

How to disable the mousedown action for clicks on the link?

Thank!

Update: . Based on the responses, I eventually included the following JavaScript at the end of the HTML, which registered an event handler to stop the distribution, as suggested:

...
<script type="text/javascript" language="javascript">
//<![CDATA[
function stopEvent(ev) {
    ev.stopPropagation();
}
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
    links[i].addEventListener("onmousedown", stopEvent, false);
}
//]]>
</html>

This could easily be included in the onload event of the document, but the CDATA section was already needed for other reasons, so I added what I need here.

Thanks for the help and good suggestions! :)

+3
source share
2 answers

If you use jQuery, it would be so simple (suppose you have set and id = 'row' for the tr element, and id = 'link' for the element):

$('#row').click(someaction);
$('#link').click(function(event){event.stopPropagation()});

You can look at this example: http://jsfiddle.net/VDQMm/

jQuery , , addEventListener ( , false), old event.stopPropagation().

- (http://jsfiddle.net/VDQMm/2):

var row = document.getElementById('row');
var link = document.getElementById('link');
row.addEventListener('click',alert('clicked row!'),false)
link.addEventListener('click',function(event){event.stopPropagation()},false)
+5

event.stopPropagation() mousedown . tr.

.

+1

Source: https://habr.com/ru/post/1779676/


All Articles