Commonplace use of the jquery data method _jquery

Source: Internet
Author: User

(-1) description

I'm using chrome49, this method involves the jquery version problem, I have 3.0 in my hand, 1.9.1, then 1.9.1 and the previous one is called the lower version, 3.0 is called the high version

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)

The key value is a string type, a number, object (Var lol={}), is meaningless, the value can be a normal type, and is passed as a reference rather than a clone when it is a reference type

A.

var obj = {"Name": "Ice Shooter", "Age": "12"};
$.data (obj, "height", 165);
ShowMessage ($.data (obj, "height"));

$.data actually put the data on the object properties, similar to the obj.sex= "female", whether you add a few keys to obj through $.data, in obj only one more similar to the jQuery300082932543555993442

300082932543555993442 is the random number generated by jquery, which points to an object that stores the data you add in the form of Key-value.

The lower version can traverse the for-in through the jQuery300082932543555993442, the high version can only be viewed through the Debug tool

B.

When value is an object, a reference is passed

var husband = ["King of Barbarians"];
var obj = {"Name": "Ice Shooter", "Age": "12"};
$.data (obj, "husband", husband);
$.data (Obj,husband) [0] = "dun tornado";
ShowMessage ($.data (Obj,husband));/Output tornado

(2) $ (selector). Data (Key,value)

$ ("#lol"). Data ("name", "Ice Shooter");

ShowMessage ($ ("#lol"). Data ("name");

$ (selector). Data binds to HTML DOM elements, and the remaining features are the same as the $.data method, but you can get the added objects by for-in

The use of this old jquery data method is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.