How to prevent reload with onclick without quot;#quot;?(如何防止在没有“#的情况下使用 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 重新加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!