gpt4 book ai didi

javascript - 超链接 div 内的按钮

转载 作者:行者123 更新时间:2023-11-28 14:09:46 25 4
gpt4 key购买 nike

我有一个带有超链接的元素。里面还有按钮,它们应该触发其按钮操作,而不触发超链接。

演示代码:

<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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com