.load() | Hàm JQuery | Tham Khảo JQuery | Học Web Chuẩn

.load()
  • Trang chủ
  • Tham khảo
  • jQuery
  • jQuery - function
  • .load()

Định nghĩa và sử dụng

Có 2 cách sử dụng:

  • Load sự kiện javascript.
  • load dữ liệu từ server sau đó đặt HTML trở lại từ các thành phần được chọn.

Cấu trúc

  • Đã được thêm vào từ phiên bản 1.0

.load(function(){...})

$('img').load(function(){...});

.load('url')

$('#test').load('/ajax/test.html');

.load('url',function(){...})

$('#test').load('/ajax/test.html',function(){...});

.load(function(){...})

Html viết:

<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Tiêu đề</title> <script src="https://code.jquery.com/jquery-latest.js"></script> <script> $(function(){ $('img').load(function(){ if($(this).height() > 100) { $(this).css('border','5px solid blue'); } }) }); </script> </head> <body> <p><img src="http://www.hocwebchuan.com/common/images/img_webstandard.gif" alt="" /></p> <p><img src="http://www.hocwebchuan.com/common/images/img_logo.gif" alt="" /></p> </body> </html>

Hiển thị trình duyệt:

Click để xem kết quả.

So sánh code HTML trước và sau khi có jQuery:

Trước khi có jQuery Sau khi có jQuery

<p><img src="http://www.hocwebchuan.com/common/images/img_webstandard.gif" alt="" /></p> <p><img src="http://www.hocwebchuan.com/common/images/img_logo.gif" alt="" /></p>

<p><img style="border: 5px solid blue;" src="http://www.hocwebchuan.com/common/images/img_webstandard.gif" alt="" /></p> <p><img src="http://www.hocwebchuan.com/common/images/img_logo.gif" alt="" /></p>

.load('url')

Html viết:

<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Tiêu đề</title> <script src="https://code.jquery.com/jquery-latest.js"></script> <script> $(function(){ $('button').click(function(){ $('div').load('/taiLieu/test.txt'); }); }); </script> </head> <body> <div>Thành phần div</div> <button>Click</button> </body> </html>

Nội dung file test.txt

Đây là dòng text của test.txt

Hiển thị trình duyệt:

Click vào button để thấy kết quả.

So sánh code HTML trước và sau khi có jQuery:

Trước khi có jQuery Sau khi có jQuery

<div>Thành phần div</div> <button>Click</button>

<div style="display: none;">Thành phần div</div> <button>Click</button>

Ví dụ thêm - load một nội dung cụ thể trong file

Html viết:

<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Tiêu đề</title> <script src="https://code.jquery.com/jquery-latest.js"></script> <script> $(function(){ $('div').load('/taiLieu/test.html #list'); }); </script> </head> <body> <div>Thành phần div</div> </body> </html>

Nội dung file test.html

<p>Nội dung không được load.</p> <ul id="list"> <li>Thành phần li 01</li> <li>Thành phần li 02</li> <li>Thành phần li 03</li> <li>Thành phần li 04</li> <li>Thành phần li 05</li> </ul>

Hiển thị trình duyệt:

So sánh code HTML trước và sau khi có jQuery:

Trước khi có jQuery Sau khi có jQuery

<div>Thành phần div</div>

<div><ul id="list"> <li>Thành phần li 01</li> <li>Thành phần li 02</li> <li>Thành phần li 03</li> <li>Thành phần li 04</li> <li>Thành phần li 05</li> </ul></div>

.load('url',function(){...})

Html viết:

<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Tiêu đề</title> <script src="https://code.jquery.com/jquery-latest.js"></script> <script> $(function(){ $('button').click(function(){ $('div').load('/taiLieu/test.txt', function() { alert('Thành phần đã được load.'); }); }); }); </script> </head> <body> <div>Thành phần div</div> <button>Click</button> </body> </html>

Nội dung file test.txt

Đây là dòng text của test.txt

Hiển thị trình duyệt:

Click vào button để thấy kết quả.

So sánh code HTML trước và sau khi có jQuery:

Trước khi có jQuery Sau khi có jQuery

<div>Thành phần div</div> <button>Click</button>

<div style="display: none;">Thành phần div</div> <button>Click</button>

.live()

.map()

HTML & XHTML

HƯỚNG DẪN HỌC

  • Hướng dẫn học
  • Hướng dẫn học XHTML & HTML5
  • Hướng dẫn học CSS
  • Hướng dẫn học CSS3
  • Hướng dẫn học Responsive
  • Hướng dẫn học ES6
  • Hướng dẫn học React.js
  • Hướng dẫn học jQuery
  • Hướng dẫn học PHP
  • Hướng dẫn học Laravel
  • Hướng dẫn học Wordpress
  • Hướng dẫn học Webpack
  • Hướng dẫn học SCSS

THAM KHẢO

Hàm jQuery (Selectors)

  • .add()
  • .addBack()
  • .addClass()
  • .after()
  • .andSelf()
  • .animate()
  • .append()
  • .appendTo()
  • .attr()
  • .before()
  • .bind()
  • .blur()
  • .change()
  • .children()
  • .clearQueue()
  • .click()
  • .clone()
  • .closest()
  • .contents()
  • .context
  • .css()
  • .dblclick()
  • .delay()
  • .delegate()
  • .dequeue()
  • .detach()
  • .die()
  • .each()
  • .empty()
  • .end()
  • .eq()
  • .error()
  • .fadeIn()
  • .fadeOut()
  • .fadeTo()
  • .fadeToggle()
  • .filter()
  • .find()
  • .finish()
  • .first()
  • .focus()
  • .focusin()
  • .focusout()
  • .get()
  • .has()
  • .hasClass()
  • .height()
  • .hide()
  • .hover()
  • .html()
  • .index()
  • .innerHeight()
  • .innerWidth()
  • .insertAfter()
  • .insertBefore()
  • .is()
  • .jquery
  • .keydown()
  • .keypress()
  • .keyup()
  • .last()
  • .length
  • .live()
  • .load()
  • .map()
  • .mousedown()
  • .mouseenter()
  • .mouseleave()
  • .mousemove()
  • .mouseout()
  • .mouseover()
  • .mouseup()
  • .next()
  • .nextAll()
  • .nextUntil()
  • .not()
  • .off()
  • .offset()
  • .offsetParent()
  • .on()
  • .one()
  • .outerHeight()
  • .outerWidth()
  • .parent()
  • .parents()
  • .parentsUntil()
  • .position()
  • .prepend()
  • .prependTo()
  • .prev()
  • .prevAll()
  • .prevUntil()
  • .remove()
  • .removeAttr()
  • .removeClass()
  • .removeData()
  • .removeProp()
  • .replaceAll()
  • .replaceWith()
  • .resize()
  • .scroll()
  • .scrollLeft()
  • .scrollTop()
  • .select()
  • .selector()
  • .serialize()
  • .serializeArray()
  • .show()
  • .siblings()
  • .size()
  • .slice()
  • .slideDown()
  • .slideToggle()
  • .slideUp()
  • .stop()
  • .submit()
  • .text()
  • .toArray()
  • .toggle()
  • .toggleClass()
  • .trigger()
  • .triggerHandler()
  • .unbind()
  • .undelegate()
  • .unload()
  • .unwrap()
  • .val()
  • .width()
  • .wrap()
  • .wrapAll()
  • .wrapInner()

Tham khảo CSS

  • CSS - Tham khảo
  • CSS - Bộ chọn (selectors)
  • CSS - Thuộc tính
  • CSS3 - Tham khảo
  • CSS3 - Bộ chọn (selectors)
  • CSS3 - Thuộc tính
  • Xem thêm ví dụ về CSS

Tham khảo HTML/XHTML

  • Tham khảo HTML/XHTML
  • Tag theo function
  • Tag theo giá trị DTD
  • Tham khảo HTML4/XHTML
  • Tham khảo HTML5
  • Xem thêm ví dụ về HTML

Tham khảo JQUERY

  • jQuery - Tham khảo
  • jQuery - Cài đặt và sử dụng
  • jQuery - Bộ chọn (selectors)
  • jQuery - Hàm (function)
  • jQuery - Ajax

Tham khảo Thêm

  • Lang codes
  • Font chữ
  • Mã ký tự
  • MIME
  • Đơn vị trong HTML & CSS
  • Giá trị màu trong HTML & CSS
  • Thuộc tính tổng quát

CHUYÊN ĐỀ

  • Chuyên đề
  • Chuyên đề HTML/CSS
  • Chuyên đề HTML5/CSS3
  • Chuyên đề jQuery/JS
  • jQuery/JS plugin

GÓP Ý - LIÊN HỆ

  • BẠN SẼ TÀI TRỢ?DONATE
  • CÔNG CỤ TẠO CSS3CSS3 GENERATOR
  • BỘ CÔNG CỤGENERATOR TOOLS
  • CÔNG CỤ HỖ TRỢWEB TOOLS
  • CÔNG CỤ TẠO RANDOMRANDOM GENERATOR
  • CÔNG CỤ KIỂM TRA RESPONSIVE TEST
  • CHIA SẺ HAYWEB & TOOLS
  • Follow us on hocwebchuan.com
  • Short URL
  • Rabbie theme

Từ khóa » Hàm Load Trong Jquery