admin管理员组文章数量:1335561
I have a javascript function that is called when a link is clicked. However I just want the function to run without the link redirecting. I have heard I should use return false but it doesn't work!
function hideaddclasses(e){
var element = document.getElementById("addclasses");
if(element.style.display=="none"){
element.style.display="";
}else{
element.style.display="none";
}
e.preventDefault();
return false;
}
Then for the html I simply have:
<a href='#' onclick='hideaddclasses()'>[ Hide ]</a>
Why does it still redirect?
I have a javascript function that is called when a link is clicked. However I just want the function to run without the link redirecting. I have heard I should use return false but it doesn't work!
function hideaddclasses(e){
var element = document.getElementById("addclasses");
if(element.style.display=="none"){
element.style.display="";
}else{
element.style.display="none";
}
e.preventDefault();
return false;
}
Then for the html I simply have:
<a href='#' onclick='hideaddclasses()'>[ Hide ]</a>
Why does it still redirect?
Share Improve this question asked Feb 7, 2012 at 3:55 Eric SmithEric Smith 1,3765 gold badges19 silver badges33 bronze badges 1- 1 The return false; may have to be in the "onclick" itself: onclick='hideaddclasses(); return false;' – Brad Commented Feb 7, 2012 at 3:56
2 Answers
Reset to default 7<a href='#' onclick='return hideaddclasses()'>[ Hide ]</a>
(Note the return
).
You can use e.preventDefault()
本文标签: javascriptstopping onclick from redirectingStack Overflow
版权声明:本文标题:javascript - stopping onclick from redirecting - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742345658a2457468.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论