Instructions on CSS operations in jquery

Source: Internet
Author: User

I just read jquery's Source code Among them, the Operation ideas about CSS and classname are really good and worth learning.
For details about jquery. classname. the definition part of has is implemented using regular expressions. In fact, if you directly use the indexof method of the string object in Javascript for processing, it is more efficient than using regular expressions. CopyCode The Code is as follows: the definition of jquery. classname. Has can be improved:
Has: function (T, C ){
T = T. classname | T;
T = "" + T + "";
C = "" + C + "";
Return T. indexof (c)>-1;
}

The operations on CSS and classname in the original code are excerpted as follows:Copy codeThe Code is as follows: classname :{
// Internal only, use addclass ("class ")
Add: function (ELEM, c ){
Jquery. Each (C. Split (/\ s +/), function (I, cur ){
If (! Jquery. classname. Has (ELEM. classname, cur ))
ELEM. classname + = (ELEM. classname? "": "") + Cur;
});
},

// Internal only, use removeclass ("class ")
Remove: function (ELEM, c ){
ELEM. classname = C?
Jquery. grep (ELEM. classname. Split (/\ s +/), function (cur ){
Return! Jquery. classname. Has (C, cur );
}). Join (""):"";
},

// Internal only, use is (". Class ")
Has: function (T, C ){
T = T. classname | T;
// Escape RegEx characters
C = C. Replace (/([\. \ + \*\? \ [\ ^ \] \ $ \ (\) \ {\}\= \! \<\>\|\:])/G, "\\$ 1 ");
Return T & New Regexp ("(^ | \ s)" + C + "(\ s | $)"). Test (t );
}
},
Swap: function (E, O, F ){
For (var I in O ){
E. Style ["old" + I] = E. Style [I];
E. Style [I] = O [I];
}
F. Apply (E, []);
For (var I in O)
E. Style [I] = E. Style ["old" + I];
},

CSS: function (E, P ){
If (P = "height" | P = "width "){
VaR old = {}, oheight, owidth, D = ["TOP", "bottom", "right", "Left"];

Jquery. Each (D, function (){
Old ["padding" + This] = 0;
Old ["border" + This + "width"] = 0;
});

Jquery. Swap (E, old, function (){
If (jquery.css (E, "display ")! = "NONE "){
Oheight = E. offsetheight;
Owidth = E. offsetwidth;
} Else {
E = jquery (E. clonenode (true ))
. Find (": Radio"). removeattr ("checked"). End ()
. CSS ({
Visibility: "hidden", position: "absolute", display: "Block", right: "0", left: "0"
}). Appendto (E. parentnode) [0];

VaR parpos = jquery.css (E. parentnode, "position ");
If (parpos = "" | parpos = "static ")
E. parentnode. style. Position = "relative ";

Oheight = E. clientheight;
Owidth = E. clientwidth;

If (parpos = "" | parpos = "static ")
E. parentnode. style. Position = "static ";

E. parentnode. removechild (E );
}
});

Return P = "height "? Oheight: owidth;
}

Return jquery. curcss (E, P );
},

Curcss: function (ELEM, prop, force ){
VaR ret;

If (prop = "opacity" & jquery. browser. MSIE)
Return jquery. ATTR (ELEM. style, "opacity ");

If (prop = "float" | prop = "cssfloat ")
Prop = jquery. browser. MSIE? "Stylefloat": "cssfloat ";

If (! Force & ELEM. Style [prop])
Ret = ELEM. Style [prop];

Else if (document. defaultview & document. defaultview. getcomputedstyle ){

If (prop = "cssfloat" | prop = "stylefloat ")
Prop = "float ";

Prop = prop. Replace (/([A-Z])/g, "-$1"). tolowercase ();
VaR cur = Document. defaultview. getcomputedstyle (ELEM, null );

If (cur)
Ret = cur. getpropertyvalue (PROP );
Else if (prop = "display ")
Ret = "NONE ";
Else
Jquery. Swap (ELEM, {display: "Block"}, function (){
VaR c = Document. defaultview. getcomputedstyle (this ,"");
Ret = C & C. getpropertyvalue (PROP) | "";
});

} Else if (ELEM. currentstyle ){

VaR newprop = prop. Replace (/\-(\ W)/g, function (M, c) {return C. touppercase ();});
Ret = ELEM. currentstyle [prop] | ELEM. currentstyle [newprop];

}

Return ret;
},

Appendix:
Jquery Official Website: http://jquery.com/
Jquery source code download: http://docs.jquery.com/Downloading_jQuery
Jquery API documentation: http://docs.jquery.com/Main_Page
Jquery China: http://jquery.org.cn/
Visualjquery.com: http://visualjquery.com/

Related Article

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.