Home  >  Article  >  Web Front-end  >  Introduction and examples of usage of jQuery.data().

Introduction and examples of usage of jQuery.data().

巴扎黑
巴扎黑Original
2017-06-20 09:57:331109browse

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

##$(selector).data(

object)

I use chrome49, this method involves the

JQuery version issue. I have 3.0 and 1.9.1. Later, 1.9.1 and its previous versions will be called low versions, and 3.0 will be called high versions

The showMessage method used in the test example is as follows

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 jQuery300082932543555993442

300082932543555993442 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-value

Lower versions can be traversed through for-in Out of jQuery300082932543555993442, higher versions can only be viewed through the debug tool

b.

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

data to the HTML DOM element. The remaining features are the same as the $.data method, but the added object can be obtained through for-in

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn