admin管理员组文章数量:1391955
I've added a button inside a link tag, with a little cross image, that I will eventually use to actually remove that element without it following the link also:
function detectClick(message) {
window.alert("Detected: " + message);
}
<div><a style="border: solid 1px black; vertical-align: middle; display:inline-block;" href=""><button style="position: relative; float: right;" onclick="detectClick('Google')"><img src="cross.png"></button>Google</a></div>
I've added a button inside a link tag, with a little cross image, that I will eventually use to actually remove that element without it following the link also:
function detectClick(message) {
window.alert("Detected: " + message);
}
<div><a style="border: solid 1px black; vertical-align: middle; display:inline-block;" href="http://www.google."><button style="position: relative; float: right;" onclick="detectClick('Google')"><img src="cross.png"></button>Google</a></div>
How do I consume that click, once it has executed detectClick(...), in order to prevent navigation away from the current page?
NOTE: Ideally, without using jQuery.
Share Improve this question edited Mar 6, 2017 at 9:34 Sumit patel 3,90311 gold badges37 silver badges65 bronze badges asked Mar 6, 2017 at 9:29 HomerPlataHomerPlata 1,7975 gold badges23 silver badges42 bronze badges 3-
return false
? – Matteo Tassinari Commented Mar 6, 2017 at 9:30 - Then take the link out all together and just have the button. – Patrick Evans Commented Mar 6, 2017 at 9:36
- Patrick, the link still has to be clickable unless you click the cross button. – HomerPlata Commented Mar 6, 2017 at 9:39
5 Answers
Reset to default 4First change your function to:
function detectClick(message){
window.alert("Detected: " + message);
return false; // add this line
}
then change the onclick
handler to:
onclick="return detectClick('Google')" // note the "return"
Please do note that AFAIK the HTML standard does not allow to have a button inside an anchor.
<script type="text/javascript">
function detectClick(message){
window.alert("Detected: " + message);
return false;
}
</script>
You need return false;
at the end of detectClick
and you need to return detectClick on your onclick event.
Explanation: The return
value of the event handler tells the browser whether the default browser action should occur. Since click
ing on your button by default triggers the click
event of its parent, the link, return false;
will prevent that default from happening, which is your exact intention.
Remove your href tag from link tag. Add a id to that link tag. And assign the href value in the detectClict(). like-->
<html>
<head>
<title>Consume Click Test</title>
</head>
<body>
<div><a id = "link" style="border: solid 1px black; vertical-align:middle; display:inline-block;" > <button style="position: relative; float: right;" onclick="detectClick('Google')"><img src="cross.png"></button>Google</a> </div>
</body>
</html>
<script type="text/javascript">
function detectClick(message){
window.alert("Detected: " + message);
document.getElementById("link").href = "http://www.google.";
}
</script>
<html>
<head>
<title>Consume Click Test</title>
</head>
<body>
<div><a style="border: solid 1px black; vertical-align: middle; display:inline-block;" href="javascript:void 0;"><button style="position: relative; float: right;" onclick="detectClick('Google')"><img src="cross.png"></button>Google</a></div>
</body>
</html>
<script type="text/javascript">
function detectClick(message){
window.alert("Detected: " + message);
}
</script>
本文标签: javascriptHow to consume button click inside ltagt tag to prevent link being followedStack Overflow
版权声明:本文标题:javascript - How to consume button click inside <a> tag to prevent link being followed? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744715700a2621390.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论