jQuery中的jQuery.load()和jQuery.get()方法有什么区别?

jQueryload()方法

load(url,data,callback)方法从服务器加载数据,并将返回的HTML放入匹配的元素中。

这是此方法使用的所有参数的描述-

  • url- 一个包含请求发送到的URL的字符串。

  • data- 此可选参数表示与请求一起发送的数据映射。

  • callback- 此可选参数表示如果请求成功将执行的功能

假设我们在result.html文件中包含以下HTML内容:

<h1>THIS IS RESULT...</h1>

示例

以下是显示此方法用法的代码段。

   <head>
      <script src = "https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {
            $("#driver").click(function(event){
               $('#stage').load('result.html');
            });
         });
      </script>
   </head>
   
   <body>
   
      <p>Click on the button to load result.html file:</p>
       
      <div id = "stage" style = "background-color:cc0;">
         STAGE
      </div>
       
      <input type = "button" id = "driver" value = "Load Data" />
       
   </body>

jQueryget()方法

jQuery.get(url,[data],[callback],[type])方法使用GET HTTP请求从服务器加载数据。

这是此方法使用的所有参数的描述-

  • url- 包含请求发送到的URL的字符串

  • 数据 -此可选参数表示将发送到服务器的键/值对。

  • callback- 此可选参数表示每当成功加载数据时将执行的函数。

  • type- 此可选参数表示要返回给回调函数的数据类型:“ xml”,“ html”,“ script”,“ json”,“ jsonp”或“ text”。

假设我们在result.php文件中包含以下PHP内容,

<html>
   <?php
    if( $_REQUEST["name"] ) {

      $name = $_REQUEST['name'];
      echo "Welcome ". $name;
    }
   ?>
</html>

示例

以下是显示此方法用法的代码片段-

   <head>
      <script src = "https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {
           
            $("#driver").click(function(event){
               $.get(
                  "result.php",
                  { name: "Zara" },
                  function(data) {
                     $('#stage').html(data);
                  }
               );
            });
               
         });
      </script>    
   </head>
   
   <body>
   
      <p>Click on the button to load result.html file</p>
       
      <span id = "stage" style = "background-color:#cc0;">
         STAGE
      </span>
       
      <div><input type = "button" id = "driver"
         value = "Load Data" /></div>
   
   </body>