您现在的位置是:永利皇宫网页入口-yl6809永利官网 > 网站收录
jquery 中关于css操作部分使用说明-永利皇宫网页入口
襄阳襄助网络有限公司2024-08-19 00:20:13【网站收录】1人已围观
简介襄阳襄助网络有限公司(www.scgcj09.com)成立于2012年,致力于提供高品质的住宅和商业空间清洁服务,确保环境整洁卫生。家政电话:13114448813,欢迎来电。
刚刚看了下jquery的中关源代码,其中关于css及classname的操作操作思想确实很不错,值得借鉴。部分 其中关于jquery.classname.has的使用说明定义部分,是中关用的正则来实现的,其实此处直接利用javascript中string对象的操作indexof方法来作处理的话,比用正则效率会更些,部分因此

jquery.classname.has的使用说明定义可以改进成:

has:function(t,c){

t=t.classname||t;
t="" t "";
c="" c "";
returnt.indexof(c)>-1;
}
原代码中关于css及classname的操作部分节选如下:
classname:{
//internalonly,useaddclass("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;
});
},
//internalonly,useremoveclass("class")
remove:function(elem,c){
elem.classname=c?
jquery.grep(elem.classname.split(/\s /),function(cur){
return!jquery.classname.has(c,cur);
}).join(""):"";
},
//internalonly,useis(".class")
has:function(t,c){
t=t.classname||t;
//escaperegexcharacters
c=c.replace(/([\.\\\ \*\?\[\^\]\$\(\)\{ \}\=\!\<\>\|\:])/g,"\\$1");
returnt&&newregexp("(^|\\s)" c "(\\s|$)").test(t);
}
},
swap:function(e,o,f){
for(variino){
e.style["old" i]=e.style[i];
e.style[i]=o[i];
}
f.apply(e,[]);
for(variino)
e.style[i]=e.style["old" i];
},
css:function(e,p){
if(p=="height"||p=="width"){
varold={ },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];
varparpos=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);
}
});
returnp=="height"?oheight:owidth;
}
returnjquery.curcss(e,p);
},
curcss:function(elem,prop,force){
varret;
if(prop=="opacity"&&jquery.browser.msie)
returnjquery.attr(elem.style,"opacity");
if(prop=="float"||prop=="cssfloat")
prop=jquery.browser.msie?"stylefloat":"cssfloat";
if(!force&&elem.style[prop])
ret=elem.style[prop];
elseif(document.defaultview&&document.defaultview.getcomputedstyle){
if(prop=="cssfloat"||prop=="stylefloat")
prop="float";
prop=prop.replace(/([a-z])/g,"-$1").tolowercase();
varcur=document.defaultview.getcomputedstyle(elem,null);
if(cur)
ret=cur.getpropertyvalue(prop);
elseif(prop=="display")
ret="none";
else
jquery.swap(elem,{ display:"block"},function(){
varc=document.defaultview.getcomputedstyle(this,"");
ret=c&&c.getpropertyvalue(prop)||"";
});
}elseif(elem.currentstyle){
varnewprop=prop.replace(/\-(\w)/g,function(m,c){ returnc.touppercase();});
ret=elem.currentstyle[prop]||elem.currentstyle[newprop];
}
returnret;
},
附录:
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/
实现代码如下:

jquery.classname.has的使用说明定义可以改进成:

has:function(t,c){

t=t.classname||t;
t="" t "";
c="" c "";
returnt.indexof(c)>-1;
}
原代码中关于css及classname的操作部分节选如下:
实现代码如下:
classname:{
//internalonly,useaddclass("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;
});
},
//internalonly,useremoveclass("class")
remove:function(elem,c){
elem.classname=c?
jquery.grep(elem.classname.split(/\s /),function(cur){
return!jquery.classname.has(c,cur);
}).join(""):"";
},
//internalonly,useis(".class")
has:function(t,c){
t=t.classname||t;
//escaperegexcharacters
c=c.replace(/([\.\\\ \*\?\[\^\]\$\(\)\{ \}\=\!\<\>\|\:])/g,"\\$1");
returnt&&newregexp("(^|\\s)" c "(\\s|$)").test(t);
}
},
swap:function(e,o,f){
for(variino){
e.style["old" i]=e.style[i];
e.style[i]=o[i];
}
f.apply(e,[]);
for(variino)
e.style[i]=e.style["old" i];
},
css:function(e,p){
if(p=="height"||p=="width"){
varold={ },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];
varparpos=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);
}
});
returnp=="height"?oheight:owidth;
}
returnjquery.curcss(e,p);
},
curcss:function(elem,prop,force){
varret;
if(prop=="opacity"&&jquery.browser.msie)
returnjquery.attr(elem.style,"opacity");
if(prop=="float"||prop=="cssfloat")
prop=jquery.browser.msie?"stylefloat":"cssfloat";
if(!force&&elem.style[prop])
ret=elem.style[prop];
elseif(document.defaultview&&document.defaultview.getcomputedstyle){
if(prop=="cssfloat"||prop=="stylefloat")
prop="float";
prop=prop.replace(/([a-z])/g,"-$1").tolowercase();
varcur=document.defaultview.getcomputedstyle(elem,null);
if(cur)
ret=cur.getpropertyvalue(prop);
elseif(prop=="display")
ret="none";
else
jquery.swap(elem,{ display:"block"},function(){
varc=document.defaultview.getcomputedstyle(this,"");
ret=c&&c.getpropertyvalue(prop)||"";
});
}elseif(elem.currentstyle){
varnewprop=prop.replace(/\-(\w)/g,function(m,c){ returnc.touppercase();});
ret=elem.currentstyle[prop]||elem.currentstyle[newprop];
}
returnret;
},
附录:
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/
以上就是【jquery 中关于css操作部分使用说明】的全部内容了,欢迎留言评论进行交流!
很赞哦!(745)
上一篇: 领取抖音双11攻略(领取方法及使用技巧)