如何用jQuery刷新一个页面

如何用jQuery刷新一个页面

方法1:使用location.reload() : location.reload()方法模拟点击浏览器上的刷新按钮来重新加载当前的网页。传递给该方法的可选参数true用于强制从服务器上加载页面,而忽略浏览器的缓存。

语法:

location.reload(true)

示例:

<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to refresh a page
        using jQuery?
    </title>
      
    <script src=
        "https://code.jquery.com/jquery-3.3.1.min.js">
    </script>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
      
    <b>
        How to refresh a page
        using jQuery?
    </b>
      
    <p>
        GeeksforGeeks is a computer science
        portal with a huge variety of well
        written and explained computer science
        and programming articles, quizzes
        and interview questions.
    </p>
      
    <button type="button">
        Button to Reload page
    
  
    <script type="text/javascript">
        (document).ready(function () {
            ("button").click(function () {
                location.reload(true);
                alert('Reloading Page');
            });
        });
    </script>
</body>
  
</html>                    

方法2:使用history.go(0): history.go()方法根据传递给它的参数,从浏览器的历史中加载一个URL。如果传递的参数是’0’,它将重新加载当前页面。

语法:

history.go(0);

示例:

<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to refresh a page
        using jQuery?
    </title>
      
    <script src=
        "https://code.jquery.com/jquery-3.3.1.min.js">
    </script>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
      
    <b>
        How to refresh a page
        using jQuery?
    </b>
      
    <p>
        GeeksforGeeks is a computer science
        portal with a huge variety of well
        written and explained computer science
        and programming articles, quizzes
        and interview questions.
    </p>
      
    <button type="button">
        Button to Reload page
    
  
    <script type="text/javascript">
        (document).ready(function () {
            ("button").click(function () {
                history.go(0);
                alert('Reloading Page');
            });
        });
    </script>
</body>
  
</html>                    

方法3:使用location.replace与当前页面: location.replace()方法可以用location.pathname作为参数。location.pathname返回当前的url,并将其传递给location.replace(),重新加载当前页面。

语法:

location.replace(location.pathname);

示例:

<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to refresh a page
        using jQuery?
    </title>
      
    <script src=
        "https://code.jquery.com/jquery-3.3.1.min.js">
    </script>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
      
    <b>
        How to refresh a page
        using jQuery?
    </b>
      
    <p>
        GeeksforGeeks is a computer science
        portal with a huge variety of well
        written and explained computer science
        and programming articles, quizzes
        and interview questions.
    </p>
      
    <button type="button">
        Button to Reload page
    
  
    <script type="text/javascript">
        (document).ready(function () {
            ("button").click(function () {
                location.reload(true);
                alert('Reloading Page');
            });
        });
    </script>
</body>
  
</html>                    

输出:

  • 在点击按钮之前:

如何用jQuery刷新一个页面

  • 点击按钮后:

如何用jQuery刷新一个页面

jQuery是一个开源的JavaScript库,它简化了HTML/CSS文档之间的交互,它以其 “少写多做 “的理念而广为人知。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程