Jquery : load() trong jQuery ajax

0
0
(0)


Định nghĩa load() trong jQuery

Phương thức load(url,data,callback) trong jQuery tải dữ liệu từ server và đặt HTML đã trả về vào trong phần tử được so khớp.

Cú pháp load() trong jQuery

Sau đây là cú pháp cho load(url,data,callback) trong jQuery:

[selector].load( url, [data], [callback] )

Tham số

Dưới đây miêu tả chi tiết về các tham số được sử dụng trong phương thức load(url,data,callback) trong jQuery:

  • url − Một chuỗi chứa URL để request được gởi tới
  • data − tham số tùy ý này biểu diễn một map của dữ liệu được gửi với request.
  • callback − tham số tùy ý này biểu diễn một hàm mà được thực thi nếu request thành công.

Ví dụ load() trong jQuery

Giả sử chúng ta có nội dung HTML sau trong result.html file:

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

Sau đây là ví dụ đơn giản minh họa cách sử dụng của phương thức load(url,data,callback) trong jQuery:


<html>
   <head>
      <title>The jQuery Example</title>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

      <script type="text/javascript" language="javascript">
         $(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>

</html>

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave A Reply

Your email address will not be published.