Instructions for CSS operations in jQuery _ jquery

Source: Internet
Author: User
The usage instructions on CSS operations in jQuery have just read the jQuery source code. The operations on 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.

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:

The 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 site: http://jquery.com/href = "http://jquery.com/" target = _ blank> http://jquery.com/
JQuery source code download: http://docs.jquery.com/Downloading_jQuery href = "http://docs.jquery.com/Downloading_jQuery" target = _ blank> http://docs.jquery.com/Downloading_jQuery
JQuery API documentation: http://docs.jquery.com/Main_Page href = "http://docs.jquery.com/Main_Page" target = _ blank> http://docs.jquery.com/Main_Page
JQuery China: http://jquery.org.cn/href = "http://jquery.org.cn/" target = _ blank> http://jquery.org.cn/
VisualJQuery.com: http://visualjquery.com/href = "http://visualjquery.com/" target = _ blank> 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.