当子元素的onclick触发时,是否可以阻止浏览器跟踪链接? [英] Is it possible to stop the browser from following the link when the onclick of the child element fires?
问题描述
这基本上就是我所拥有的:
This is what I have basically:
<a href="http://somelink.com">
<span>stuff</span>
<span onclick="AwesomeFunction();">more stuff</span>
<span>the last stuff</span>
</a>
现在,问题是我想将父级保留为链接但是如果用户点击了跨度使用onclick事件我不希望浏览器关注链接。
Now, the problem is I want to keep the parent as a link but if the user clicks the span with the onclick event I don't want the browser to follow the link.
我试过了
event.stopPropagation();
但这似乎只会阻止点击onclick事件,或者我做错了什么。
but that only seems to stop the links onclick event from firing, or I'm doing something wrong.
我目前处于危机模式,我不想花太多时间重写生成这个HTML的代码,但它仍然不能黑客,因为它是在网站的一个非常重要的功能实施。任何帮助表示赞赏。
I'm currently sort of in crunch mode and I don't want to spend too much time rewriting the code that generates this HTML, but it still can't be a hack since it's implemented in a pretty vital function of the site. Any help appreciated.
推荐答案
让javascript方法返回false!
Make the javascript method return false!
或者您也可以使用 event.preventDefault()
而不是 event.stopPropagation()
Or you can also use event.preventDefault()
instead of event.stopPropagation()
这篇关于当子元素的onclick触发时,是否可以阻止浏览器跟踪链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!