Just read the source code of jquery, which on the CSS and classname operation of the idea is really good, it is worth learning.
The definition part of JQuery.className.has is implemented by the regular, in fact, the use of the IndexOf method of the string object in JavaScript is used to deal with it, which is more than the regular efficiency, so
Copy Code code as follows:
The definition of JQuery.className.has can be improved to:
Has:function (t, c) {
t = T.classname | | T
T = "" + t + "";
c = "" + C + "";
return T.indexof (c) >-1;
}
The original code in the CSS and classname operation part of the excerpt is as follows:
Copy Code code 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 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/