admin管理员组文章数量:1289539
I have a table-row 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 a user clicks anywhere in the row, I want the JavaScript action, someAction(this)
, to be followed. However, if a user clicks on the link to page2, then I want the action ignored and the link followed.
Unfortunately, the action is being observed first, which happens to move the linked text's position just enough for the click to be dropped. In other words, the user cannot actually click on the link and go to page2, in this case.
How can I disable the mousedown
action for clicks over the link?
Update: Based on the answers, I ultimately included the following JavaScript in the end of the HTML, which registered an event handler to stop propagation as suggested, like so:
...
<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 have easily been included in the document's onload
event, but the CDATA section was already required for other reasons, so I added what I needed here.
I have a table-row 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 a user clicks anywhere in the row, I want the JavaScript action, someAction(this)
, to be followed. However, if a user clicks on the link to page2, then I want the action ignored and the link followed.
Unfortunately, the action is being observed first, which happens to move the linked text's position just enough for the click to be dropped. In other words, the user cannot actually click on the link and go to page2, in this case.
How can I disable the mousedown
action for clicks over the link?
Update: Based on the answers, I ultimately included the following JavaScript in the end of the HTML, which registered an event handler to stop propagation as suggested, like so:
...
<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 have easily been included in the document's onload
event, but the CDATA section was already required for other reasons, so I added what I needed here.
2 Answers
Reset to default 5If you were using jQuery, it would be that simple (suppose you have set and id='row' for tr element, and id='link' for a element):
$('#row').click(someaction);
$('#link').click(function(event){event.stopPropagation()});
You can look at this example: http://jsfiddle/VDQMm/
Without jQuery, it is almost the same, but you have to get each element's handle first, then use addEventListener method to attach an event listener (make the row handler to use bubbling instead of capturing by setting the last parameter to false), and then the same old event.stopPropagation().
It would be something like this ( http://jsfiddle/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)
Add event.stopPropagation()
to the mousedown event of the link. That will stop the event from bubbling up to the tr
.
Here is an example.
本文标签: javascriptHow to ignore first event and skip to secondStack Overflow
版权声明:本文标题:javascript - How to ignore first event and skip to second? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741443081a2379030.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论