最近有个朋友问我了这么一个问题
a href="javascript:void(0)" onclick="fun()"
a
href="#" onclick="fun()"
a href="javascript:;"
onclick="fun()" 这三种写法的区别是什么啊?
我简单的做个回答:
javascript:;和javascript:void(0)是几乎差不多的,一个是执行空语句,另外一个
void(0)返回undefined。两个都不建议用。而带有#的会导致页面跳到顶部。
一般建议都是建议这么用:
<a href="#"
onclick="fun(); return false;">Link</a>因为return
false可以防止href里的#继续执行。