Javascript 单击时刷新页面的按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29884654/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:13:52  来源:igfitidea点击:

Button that refreshes the page on click

javascriptbuttonrefreshreload

提问by Stefan ?or?evi?

I need a button that will refresh the page on the user's click. I tried this:

我需要一个按钮,可以在用户点击时刷新页面。我试过这个:

<input type="button" value="Reload Page" onClick="reload">

or

或者

<input type="button" value="Refresh Page" onClick="refresh">

But neither worked.

但都没有奏效。

回答by CONvid19

Use onClickwith window.location.reload(), i.e. :

onClick与 一起使用window.location.reload(),即:

<button onClick="window.location.reload();">Refresh Page</button>

Or history.go(0), i.e.:

或者history.go(0),即:

<button onClick="history.go(0);">Refresh Page</button>

Or window.location.href=window.location.hreffor 'full' reload, i.e.:

或者window.location.href=window.location.href对于“完整”重新加载,即:

<button onClick="window.location.href=window.location.href">Refresh Page</button>


The Button element - developer.mozilla.org

按钮元素 - developer.mozilla.org

回答by Manjunath Akalawadi

This works for me

这对我有用

HTML

HTML

    <button type="submit"  onClick="refreshPage()">Refresh Button</button>

JavaScript

JavaScript

<script>
function refreshPage(){
    window.location.reload();
} 
</script>

回答by Awais Jameel

<a onClick="window.location.reload()">Refresh</a>

This really works perfect for me.

这对我来说真的很完美。

回答by Love Kumar

button that refresh the page on click

单击时刷新页面的按钮

Use onClick button with window.location.reload():

将 onClick 按钮与 window.location.reload() 一起使用:

<button onClick="window.location.reload();">

回答by ilias iliadis

Only this realy reloads page (Today)

只有这个真的重新加载页面(今天)

<input type="button" value="Refresh Page" onClick="location.href=location.href">

Others do not exactly reload. They keep values inside text boxes.

其他人不完全重新加载。它们将值保存在文本框中。

回答by Thielicious

<button onclick=location=URL>Refresh</button>

This might look funny but it really does the trick.

这可能看起来很有趣,但它确实有效。

回答by volt

I noticed that all the answers here use inline onClickhandlers. It's generally recommended to keep HTML and Javascript separate.

我注意到这里的所有答案都使用内联onClick处理程序。通常建议将 HTML 和 Javascript 分开。

Here's an answer that adds a click event listenerdirectly to the button. When it's clicked, it calls location.reloadwhich causes the page to reload with the current URL.

这是一个直接向按钮添加单击事件侦听器的答案。当它被点击时,它会调用location.reload这会导致页面使用当前 URL 重新加载。

const refreshButton = document.querySelector('.refresh-button');

const refreshPage = () => {
  location.reload();
}

refreshButton.addEventListener('click', refreshPage)
.demo-image {
  display: block;
}
<button class="refresh-button">Refresh!</button>
<img class="demo-image" src="https://picsum.photos/200/300">

回答by Hamid s k

just create the empty reference on link such as following

只需在链接上创建空引用,例如以下

 <a href="" onclick="dummy(0);return false;" >

回答by prashant kute

Use this its work fine for me to reload the same page:

使用它对我来说很好地重新加载同一页面:

<button onClick="window.location.reload();">

回答by Richard Taylor-Kenny

If you are looking for a form reset:

如果您正在寻找表格重置:

<input type="reset" value="Reset Form Values"/>

or to reset other aspects of the form not handled by the browser

或重置浏览器未处理的表单的其他方面

<input type="reset" onclick="doFormReset();" value="Reset Form Values"/>

Using jQuery

使用 jQuery

function doFormReset(){
    $(".invalid").removeClass("invalid");
}