jQuery Misc data()วิธี การ
ตัวอย่าง
แนบข้อมูลกับองค์ประกอบ <div> จากนั้นดึงข้อมูล:
$("#btn1").click(function(){
$("div").data("greeting", "Hello World");
});
$("#btn2").click(function(){
alert($("div").data("greeting"));
});
ความหมายและการใช้งาน
data() วิธีการแนบข้อมูลหรือรับข้อมูลจากองค์ประกอบที่เลือก
เคล็ดลับ:หากต้องการลบข้อมูล ให้ใช้เมธอดremoveData()
ส่งคืนข้อมูลจาก Element
ส่งกลับข้อมูลที่แนบจากองค์ประกอบที่เลือก
ไวยากรณ์
$(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 |
แนบข้อมูลกับองค์ประกอบ
แนบข้อมูลกับองค์ประกอบที่เลือก
ไวยากรณ์
$(selector).data(name,value)
Parameter | Description |
---|---|
name | Required. Specifies the name of data to set |
value | Required. Specifies the value of data to set |
แนบข้อมูลกับองค์ประกอบโดยใช้วัตถุ
แนบข้อมูลกับองค์ประกอบที่เลือกโดยใช้วัตถุที่มีคู่ชื่อ/ค่า
ไวยากรณ์
$(selector).data(object)
Parameter | Description |
---|---|
object | Required. Specifies an object containing name/value pairs |