Home > Article > Web Front-end > Introduction and examples of usage of jQuery.data().
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; }(0)$.data(obj,key,value)key value is of
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"));$.data actually puts the data on the object attribute, similar to obj.sex="female". No matter how many keys you add to obj through $.data, there will only be one more in obj Similar to jQuery300082932543555993442300082932543555993442 is a random number generated by jQuery. It points to an object in which the data you add is stored in the form of key-valueLower versions can be traversed through for-in Out of jQuery300082932543555993442, higher versions can only be viewed through the debug toolb.When value is an object, a reference is passed
var husband = ["蛮族之王"]; var obj = {"name":"寒冰射手","age":"12"}; $.data(obj,"husband",husband); $.data(obj,husband)[0] = "遁地龙卷风"; showMessage($.data(obj,husband));//输出 遁地龙卷风(2)
$(selector).data(key,value) $("#lol").data("name","寒冰射手"); showMessage($("#lol").data("name"));$(selector).data binds
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!