刚刚看了下jquery的源代码,其中关于css及classname的操作思想确实很不错,值得借鉴。
其中关于jquery.classname.has的定义部分,是用的正则来实现的,其实此处直接利用javascript中string对象的indexof方法来作处理的话,比用正则效率会更些,因此
复制代码 代码如下:
jquery.classname.has的定义可以改进成:
has: function( t, c ) {
t = t.classname || t;
t = + t + ;
c = + c + ;
return t.indexof(c)>-1;
}
原代码中关于css及classname的操作部分节选如下:
复制代码 代码如下:
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;
},
附录:
jquery官方网站:http://jquery.com/
jquery源码下载:http://docs.jquery.com/downloading_jquery
jquery api文档:http://docs.jquery.com/main_page
jquery 中国:http://jquery.org.cn/
visualjquery.com : http://visualjquery.com/