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


基础教程推荐
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01