我有一个带有超链接的元素。里面还有按钮,它们应该触发其按钮操作,而不触发超链接。
演示代码:
<a href="element.html">
<div class="element_card">
This is an card for an element with an hyperlink over the card.
<button type="button" onclick="like_element()">
Like the element but do not trigger the hyperlink.
</button>
</div>
</a>
如何禁用按钮的超链接或取消事件,使按钮区域只触发按钮的onclick函数而不触发超链接。
最佳答案
您可以使用event.stopPropagation()
来阻止点击沿着链向上传播。
关于javascript - 超链接 div 内的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59988104/