当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


JQuery load()用法及代码示例

jQueryload()方法是一种简单但函数非常强大的AJAX方法。 jQuery 中的Load() 方法有助于从服务器加载数据并将其返回到所选元素,而无需加载整个页面。

用法:

$(selector).load(URL, data, callback);

参数:该方法接受如上所述和如下所述的三个参数:

  • URL:用于指定需要加载的URL。
  • data:它用于指定与请求一起发送的一组查询键/值对。
  • callback:它是可选参数,是调用load()方法后要执行的函数名称。

示例 1:geeks.txt 文件存储在服务器上,单击按钮后将加载该文件。 geeks.txt的内容是:Hello GeeksforGeeks!

HTML


<!DOCTYPE html>
<html>
<head>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $("#div_content").load("gfg.txt");
            });
        });
    </script>
    <style>
        body {
            text-align: center;
        }
        .gfg {
            font-size: 40px;
            font-weight: bold;
            color: green;
        }
        .geeks {
            font-size: 17px;
            color: black;
        }
        #div_content {
            font-size: 40px;
            font-weight: bold;
            color: green;
        }
    </style>
</head>
<body>
    <div id="div_content">
        <div class="gfg">
            GeeksforGeeks
        </div>
        <div class="geeks">
            A computer science portal for geeks
        </div>
    </div>
    <button>
        Change Content
    </button>
</body>
</html>

输出:

参数中还有一个额外的回调函数,该函数将在 load() 方法完成时运行。该回调函数具有三个不同的参数:

  • 参数1:如果方法调用成功,它包含内容的结果。
  • 参数2:它包含调用函数的状态。
  • 参数3:它包含 XMLHttpRequest 对象。

示例 2:单击该按钮后将出现一个警告框,如果内容加载成功,则会显示“内容加载成功!”消息。否则,它将显示错误消息。

HTML


<!DOCTYPE html>
<html>
<head>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $("#div_content").load("gfg.txt", function (response,
                    status, http) {
                    if (status == "success")
                        alert("Content loaded successfully!");
                    if (status == "error")
                        alert("Error: " + http.status + ": "
                            + http.statusText);
                });
            });
        });
    </script>
    <style>
        body {
            text-align: center;
        }
        .gfg {
            font-size: 40px;
            font-weight: bold;
            color: green;
        }
        .geeks {
            font-size: 17px;
            color: black;
        }
        #div_content {
            font-size: 40px;
            font-weight: bold;
            color: green;
        }
    </style>
</head>
<body>
    <div id="div_content">
        <div class="gfg">
            GeeksforGeeks
        </div>
        <div class="geeks">
            A computer science portal for geeks
        </div>
    </div>
    <button>
        Change Content
    </button>
</body>
</html>

输出:



相关用法


注:本文由纯净天空筛选整理自kundankumarjha大神的英文原创作品 jQuery load() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。