$. Data Applications under $. extend and $. fn,. extend. fn
1. First, we will study $. data
1) code snippet
<Body onload = "window. scrollTo (0, 1);">
<H1 class = "th"> Welcome <Input id = "test" type = "text" data-age = "5725" name = "nam" value = "123"/>
</Body>
<Script type = "text/javascript">
$ (Function (){
Var val = $ ("# test"). data ('age ');
$ ("# Test"). data ('name', 19); // assign a value in jquery
Var val1 = $ ("# test"). data ('name ');
Console. log (val + '---' + val1 );
});
</Script>
Note: add the data-name attribute to the tag to call the value of the variable named after data-in $. data.
2) connection code, $. data Usage in $. fn
$. Extend ($. fn ,{
ShowAge: function (){
Console. log (this [0]. dataset. age );
}
});
$ ("# Test"). showAge ();
Note: Here this [0] indicates the object for reading tag attributes.
9. code snippet
<Body onload = "window. scrollTo (0, 1);">
<H1 class = "th"> Welcome
<Input type = "text" id = "uname" name = "nam" value = "123"/>
<Input type = "text" name = "name" value = "123456"/>
<Input type = "button" name = "btn" value = "vla"/>
</Body>
<Script type = "text/javascript">
$ (Function (){
Var a = {
Name: "AB ",
Func: "onely"
};
$. Extend ($. fn ,{
ShowName: function (){
Console. log ("sdfsd ");
},
EchoName: function (op ){
// $. Data (this [0], 'name', 'dataname ');
Var val = $. data (this [0], 'name ');
Console. log (val );
}
});
Var option = {
Name: "dddd"
};
$ ("# Uname"). data ('name', 'ddddd ');
$ (": Input"). echoName (option );
});
</Script>