Phương thức jQuery Misc data ()

❮ Phương thức jQuery Misc

Thí dụ

Đính kèm dữ liệu vào phần tử <div>, sau đó truy xuất dữ liệu:

$("#btn1").click(function(){
  $("div").data("greeting", "Hello World");
});
$("#btn2").click(function(){
  alert($("div").data("greeting"));
});

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

Phương thức data () gắn dữ liệu vào hoặc lấy dữ liệu từ các phần tử đã chọn.

Mẹo: Để xóa dữ liệu, hãy sử dụng phương thức removeData () .


Trả lại dữ liệu từ một phần tử

Trả về dữ liệu đính kèm từ một phần tử đã chọn.

Cú pháp

$(selector).data(name)

Parameter Description
name Optional. Specifies the name of data to retrieve.
If no name is specified, this method will return all stored data for the element as an object


Đính kèm dữ liệu vào một phần tử

Đính kèm dữ liệu vào các phần tử đã chọn.

Cú pháp

$(selector).data(name,value)

Parameter Description
name Required. Specifies the name of data to set
value Required. Specifies the value of data to set

Đính kèm dữ liệu vào một phần tử bằng một đối tượng

Đính kèm dữ liệu vào các phần tử đã chọn bằng cách sử dụng một đối tượng có các cặp tên / giá trị.

Cú pháp

$(selector).data(object)

Parameter Description
object Required. Specifies an object containing name/value pairs

❮ Phương thức jQuery Misc