如何在没有“#”的情况下使用onclick防止重新加载? [英] How to prevent reload with onclick without "#"?

查看:129
本文介绍了如何在没有“#”的情况下使用onclick防止重新加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用链接添加一些指令 - onclick调用显示简单警告框的脚本。如果我这样做...

I wish to put some instructions with a link - onclick calling a script that display a simple alert box. If I did like this...

<label for="arquivo">Máximo de 1MB, observe os <a href="" onclick="ajudaUpload();">tipos permitidos</a>.</label>

即使返回false也会重新加载页面,如果我这样做的话......

the page is reloaded even with a return false, and if I did like this...

<label for="arquivo">Máximo de 1MB, observe os <a href="#" onclick="ajudaUpload();">tipos permitidos</a>.</label>

带有#符号,页面滚动到顶部,#被添加到请求参数。有没有第三种方法可以在不重新加载,滚动和垃圾的情况下完成它?

with the "#" symbol, the page is scrolled to the top and "#" is added to query string. Is there a third way to do it without reloading, scrolling and garbage?

推荐答案

通话后返回false:

Return false after the call:

<a href="" onclick="ajudaUpload();return false;">tipos permitidos</a>

或者如果你的函数返回false,那么你可以返回函数的结果:

Or if your function returns false then you can return the result of the function:

<a href="" onclick="return ajudaUpload();">tipos permitidos</a>

仅仅在函数中返回false是不够的,你需要从点击处理程序实际返回false 。

It's not enough to just return false in the function, you need to actually return false from the click handler.

这篇关于如何在没有“#”的情况下使用onclick防止重新加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆