Jquery.data ()

Source: Internet
Author: User

data()function is used to Access data on all elements that match the current jquery object .

Parameters Description
Key The optional/string type specifies the key name string.
Value Optional/any type of data that needs to be stored.
Object Object of type specified to encapsulate multiple key-value pairs while storing multiple data.
Example One:

$ ("#btn1"). Click (function () {

$ ("div"). Data ("greeting", "Hello World"); Append data to the selected element

});

$ ("#btn2"). Click (function () {

Alert ($ ("div"). Data ("greeting")); Get the data, get the value of the key "greeting", should be Hello World

});

Example two:

Testobj=new Object ();
testobj.greetingmorn= "Good morning!";
Testobj.greetingeve= "Good evening!";

$ ("#btn1"). Click (function () {
$ ("div"). Data (Testobj);
});

$ ("#btn2"). Click (function () {
Alert ($ ("div"). Data ("Greetingeve")); Output "Good evening!"
});

Example three:

Take the following HTML code as an example:

<div Id="N1">
<div Id="N2">
<ul Id="N3">
<li Id= "N4" >item1</li>
            <li id= "N5" >item2 </li>
            < Li id= "N6" >item3</li>
        </ul>
    </div>  
</DIV>

We write the following jquery code:

Var$li=$("Li");
Store data to all LI elements at the same time
$li.Data("Name", "Codeplayer");
$li.Data("Desc", "Focus on programming development technology sharing");
$li.Data("url", "http://www.365mini.com/");

Var$n 5=$("#n5"); Data can be read from N4, N5, N6
Returns the data corresponding to the key value name
Document.Writeln($n 5.Data("Name") ); Codeplayer

Return all data as an object
VarObj=$("#n6").Data();
For(VarI in obj){
Document.Writeln(I+ "=" +Obj[I] + "<br>");
}
/* Output:
Name=codeplayer
Desc= focus on programming development technology sharing
url=http://www.365mini.com/
*/

Remove the data stored on the N4 key named name
$("#n4").Removedata("Name");
Although $li matches 3 Li elements, the read data is only subject to the first LI element N4, so it returns undefined
Document.Writeln($li.Data("Name") ); Undefined

VarObject= {
Name: "Zhang San",
Age: 18,
Score: [87, 23, 56],
Options: {Gender: Male,Address: "Water Curtain Cave" }
};

Set multiple Key-value data in object form to all DIV elements at the same time
Value values can be any type of data, including arrays, objects, and so on
$("Div").Data(Object);

Var$n 2=$("#n2"); Data can be read by N1, N2
Document.Writeln($n 2.Data( "name" ) ); //Zhang San
Document. Writeln ( $n 2. Data "score" //87,23,56
Document. Writeln ( $n 2. Data "options" //[Object Object]

Jquery.data ()

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.