如何防止超链接单击后重定向? [英] How to prevent redirect after hyperlink click?
本文介绍了如何防止超链接单击后重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下html + js代码:
I have following html+ js code:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<a href="#" id=key onclick="func(0)">foo</a>
</body>
<script type="text/javascript">
function func(k){
alert(1);
return false;
}
</script>
</html>
您能解释一下如何重构以下代码,这些代码在单击href代码 func
后执行,但是#没有添加到URL中,因此不应重新加载页面?
Can you explain how to refactor following code that after click on href code func
executes but # doesn't add to the URL and page shouldn't be reload?
推荐答案
使用 javascript:void(0);
代替#
如下:
<a href="javascript:void(0);" id=key onclick="func(0)">foo</a>
这篇关于如何防止超链接单击后重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文