jQuery 中關(guān)于CSS操作部分使用說明
更新時間:2007年06月10日 00:00:00 作者:
剛剛看了下jQuery的源代碼,其中關(guān)于CSS及className的操作思想確實很不錯,值得借鑒。
其中關(guān)于jQuery.className.has的定義部分,是用的正則來實現(xiàn)的,其實此處直接利用Javascript中String對象的indexOf方法來作處理的話,比用正則效率會更些,因此
jQuery.className.has的定義可以改進(jìn)成:
has: function( t, c ) {
t = t.className || t;
t = " " + t + " ";
c = " " + c + " ";
return t.indexOf(c)>-1;
}
原代碼中關(guān)于CSS及className的操作部分節(jié)選如下:
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官方網(wǎng)站: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/
其中關(guān)于jQuery.className.has的定義部分,是用的正則來實現(xiàn)的,其實此處直接利用Javascript中String對象的indexOf方法來作處理的話,比用正則效率會更些,因此
復(fù)制代碼 代碼如下:
jQuery.className.has的定義可以改進(jìn)成:
has: function( t, c ) {
t = t.className || t;
t = " " + t + " ";
c = " " + c + " ";
return t.indexOf(c)>-1;
}
原代碼中關(guān)于CSS及className的操作部分節(jié)選如下:
復(fù)制代碼 代碼如下:
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官方網(wǎng)站: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/
相關(guān)文章
jQuery對checkbox 復(fù)選框的全選全不選反選的操作
這篇文章主要介紹了jQuery對checkbox 復(fù)選框的全選全不選反選的操作 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08
asp.net下使用jquery 的ajax+WebService+json 實現(xiàn)無刷新取后臺值的實現(xiàn)代碼
asp.net下使用jquery 的ajax+WebService+json 實現(xiàn)無刷新取后臺值的實現(xiàn)代碼 ,比頁面刷新更好,用戶體驗更好,需要的朋友可以參考下。2010-09-09
用jQuery.ajaxSetup實現(xiàn)對請求和響應(yīng)數(shù)據(jù)的過濾
本文主要對用jQuery.ajaxSetup實現(xiàn)對請求和響應(yīng)數(shù)據(jù)的過濾的過程與方法進(jìn)行詳細(xì)全面的實例講解。具有很好的參考價值,需要的朋友一起來看下吧2016-12-12
jQuery實現(xiàn)復(fù)選框批量選擇與反選的方法
這篇文章主要介紹了jQuery實現(xiàn)復(fù)選框批量選擇與反選的方法,主要通過jQuery的attr與removeAttr方法實現(xiàn)選擇與反選的功能,非常具有實用價值,需要的朋友可以參考下2015-06-06
如何解決jQuery EasyUI 已打開Tab重新加載問題
最近在項目中遇到這樣的需求,要求實現(xiàn)點擊左側(cè)已經(jīng)打開的tab可以刷新重新加載datagrid。下面給大家分享實現(xiàn)代碼,一起看看吧2016-12-12
jQuery 淡出一個圖像到另一個圖像的實現(xiàn)代碼
這篇文章主要介紹了jquery的hover事件實現(xiàn)兩個圖片的淡出切換效果,需要的朋友可以參考下2013-06-06
基于jQuery實現(xiàn)的Ajax 驗證用戶名唯一性實例代碼
本文分為jsp代碼和后臺代碼給大家介紹了基于jQuery實現(xiàn)的Ajax 驗證用戶名唯一性,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧2017-06-06

