如何使用jQuery刷新页面?

如何使用jQuery刷新页面?下面本篇文章就来给大家介绍一下使用jQuery刷新页面的方法,希望对大家有所帮助。

方法1:使用location.reload()

location.reload()方法重新加载当前web页面,此方法类似于你浏览器上的刷新页面按钮。如果把该方法的参数设置为 true参数,可强制页面从服务器加载并忽略浏览器缓存。

语法:

location.reload(true)

示例:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

</head>
  
<body>
    <h3 style="color: green">如何使用jQuery刷新页面?</h3>
    <p>这是一段测试文本!</p>
          输入文本:<input type="text" /><br /><br />
      
    <button type="button">重新加载页面</button>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"> </script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("button").click(function () {
                location.reload(true);
                alert('Reloading Page');
            });
        });
    </script>
</body>
  
</html>

效果图:

1.gif

方法2:使用history.go(0)

history.go()方法根据传递给它的参数从浏览器历史记录中加载一个URL。如果传递的参数为“0”,则会重新加载当前页。

语法:

history.go(0);

示例:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

</head> 
  
<body> 
    <h3 style="color: green">如何使用jQuery刷新页面?</h3> 
    <p>这是一段测试文本!</p> 
          输入文本:<input type="text" /><br /><br />
      
    <button type="button">重新加载页面</button> 
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"> </script> 
    <script type="text/javascript"> 
        $(document).ready(function () { 
            $("button").click(function () { 
                history.go(0); 
            }); 
        }); 
    </script> 
</body> 
  
</html>

效果图:

2.gif

以上就是如何使用jQuery刷新页面?的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » jQuery 答疑