Definition and Usage
The data() method appends data to the selected element, or obtains data from the selected element.
Note: This is a low-level method; using .data() is more convenient.
Return data from the element
Return additional data from the selected element.
Syntax
$(selector).data(name)
Parameters Description
name Optional. Specifies the name of the data to be retrieved.
If no name is specified, this method will return all stored data from the element in the form of an object.
Append data to the element
Append data to the selected element.
Syntax
$(selector).data(name,value)
Parameters Description
name required. Specifies the name of the data to be set.
value Required. Specifies the value of the data to be set.
Use objects to append data to elements
Use objects with name/value pairs to add data to selected elements.
Syntax
object)
showMessage.i = 0; function showMessage(object) { var body = $("body")[0]; var $p =$("#debugp"); if($p.length==0) { $p = $("<p/>").attr("id","debugp"); $(body).prepend($p); } $p[0].innerHTML += "<br/>"+(showAttribute.i++)+" | "+object; }
string type, It is meaningless to be a number or object (var lol={}). The value can be a common type. When the reference type is passed, a reference is passed instead of a clone
a.var obj = {"name":"寒冰射手","age":"12"}; $.data(obj,"height",165); showMessage($.data(obj,"height"));
var husband = ["蛮族之王"]; var obj = {"name":"寒冰射手","age":"12"}; $.data(obj,"husband",husband); $.data(obj,husband)[0] = "遁地龙卷风"; showMessage($.data(obj,husband));//输出 遁地龙卷风
$(selector).data(key,value) $("#lol").data("name","寒冰射手"); showMessage($("#lol").data("name"));
The above is the detailed content of Introduction and examples of usage of jQuery.data().. For more information, please follow other related articles on the PHP Chinese website!