javascript 如何防止在没有“#”的情况下使用 onclick 重新加载?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17680436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to prevent reload with onclick without "#"?
提问by Gustavo
I wish to put some instructions with a link - onclick calling a script that display a simple alert box. If I did like this...
我希望通过链接放置一些说明 - onclick 调用显示简单警报框的脚本。如果我喜欢这个...
<label for="arquivo">Máximo de 1MB, observe os <a href="" onclick="ajudaUpload();">tipos permitidos</a>.</label>
the page is reloaded even with a return false, and if I did like this...
即使返回false,页面也会重新加载,如果我这样做......
<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?
使用“#”符号,页面滚动到顶部,“#”被添加到查询字符串中。有没有第三种方法可以在不重新加载,滚动和垃圾的情况下做到这一点?
回答by MrCode
Return false after the call:
调用后返回 false:
<a href="" onclick="ajudaUpload();return false;">tipos permitidos</a>
Or if your function returns false then you can return the result of the function:
或者,如果您的函数返回 false,那么您可以返回函数的结果:
<a href="" onclick="return ajudaUpload();">tipos permitidos</a>
It's not enough to just return false in the function, you need to actually return false from the click handler.
仅在函数中返回 false 是不够的,您需要从单击处理程序中实际返回 false。
回答by Jonny Sooter
You can use .preventDefault()
method, or return false, or remove the HREF tag all together. Either should work just fine.
您可以使用.preventDefault()
方法,或返回 false,或一起删除 HREF 标记。要么应该工作得很好。
Vc nao deviar estar usando onclick
dessa forma pra comecar. Ja eh bem antigo e nao se usa assim mais.
Vc nao deviar estar usando onclick
dessa forma pra comecar。Ja eh bem antigo e nao se usa assim mais.
回答by adityaekawade
You can use:
您可以使用:
<a href = "javascript:void(0);" onclick="ajudaUpload();">
Even I was stuck on a similar problem. I wanted to use onclick function but the page would reload, which I didn't want. I tried href = "javascript:void(0);" and it worked.
甚至我也被困在类似的问题上。我想使用 onclick 功能,但页面会重新加载,这是我不想要的。我试过 href = "javascript:void(0);" 它奏效了。
回答by Samurai
<a href="" onclick="return ajudaUpload()">tipos permitidos</a>
and then return false in your function:
然后在您的函数中返回 false:
function ajudaUpload()
{
...
return false;
}
回答by Moiz Travadi
Very simple way to do this is just add '?' (questionmark) :)
非常简单的方法就是添加“?” (问号):)
F.e.
铁
<a href="#?">Moiz Travadi</a>
回答by Nickydonna
Almost any element suports onclick events, so you can use a b tag, or a button tag, even an span tag. Then you can style it to look just like a link (a tag), or any other way you want. For example:
几乎所有元素都支持 onclick 事件,因此您可以使用 ab 标签,或按钮标签,甚至是 span 标签。然后,您可以将它的样式设置为链接(标签)或您想要的任何其他方式。例如:
<label for="arquivo">Máximo de 1MB, observe os <b onclick="ajudaUpload();">tipos permitidos</b>.</label>
回答by Kanagaraj M
Remove your herf and give id for your tag. Then by using id you can show your alert. ('#id').click(function (){alert(message); return false; });
移除你的herf并为你的标签提供id。然后通过使用 id 您可以显示您的警报。('#id').click(function (){alert(message); return false; });