First look at a piece of code:
Copy Code code as follows:
JQuery.fn.extend (
{
Myownmember:3,
Getmyownmember:function () {return this.myownmember;},
Setmyownmember:function (v) {this.myownmember = V; return this.myownmember;}
}
);
$ ("Body"). Myownmember; 3
$ ("Body"). Getmyownmember (); 3
$ ("Body"). Setmyownmember (4); 4
$ ("Body"). Getmyownmember (); 3
This code extends a member Myownmember to the JQuery object, and two function getmyownmember,setmyownmember are used to return and set the Myownmember value respectively. But we saw that Setmyownmember didn't work, and when we getmyownmember again, we returned the original value. What is this for? The reason is that $ ("body") creates a new object each time, so the Myownmember in each $ ("body") is an initial value. If we change the code to:
Copy Code code as follows:
JQuery.fn.extend (
{
Myownmember:3,
Getmyownmember:function () {return this.myownmember;},
Setmyownmember:function (v) {this.myownmember = V; return this.myownmember;}
}
);
var BODY = $ ("body");
Body.myownmember; 3
Body.getmyownmember (); 3
Body.setmyownmember (4); 4
Body.getmyownmember (); 4
That's the effect we want, because $ ("body") is created only once, followed by a reference through the body variable. However, this method in the actual use of the process is still problematic, because I can not be able to refer to the global scope of the body variable, often through the $ ("body") to get the DOM node, so how do we save a jquery object extension variable value? The solution is not to save the variable on a jquery object, but to save it on a DOM node, regardless of how many jquery objects are created on a DOM node, pointing to the same DOM node. So we'll change the code as follows:
Copy Code code as follows:
JQuery.fn.extend (
{
Getmyownmember:function () {return this[0].m Yownmember;
Setmyownmember:function (v) {this[0].myownmember = V; return this[0].myownmember;}
}
);
$ ("body"). Getmyownmember ();//undefined
$ (' body '). Setmyownmember (4);//4
$ ("body"). Getmyownmember ( ); 4