通过 jQuery 获取 background-position 的值时,在 IE 中返回 undefined
通过测试,发现无论是写在样式表中,还是写在元素style属性里,在 IE ( 6, 7, 8 ) 里获取background-position的值时,均返回undefined。
针对IE的这个bug,写了如下方法获取background-position的值:
请忽略getCSS方法,这是一个简易的获取css的方法,并不完善,了解不同浏览器获取CSS的方法
var getCSS = function(){
var rdashAlpha = /-([a-z])/ig,
fcamelCase = function( all, letter ) {
return letter.toUpperCase();
};
return function(el, name){
if (typeof el === 'string') el = document.getElementById(el);
var cssValue,
camelCase = name.replace(rdashAlpha, fcamelCase);
if (el.currentStyle){
cssValue = el.currentStyle[camelCase];
}else if(window.getComputedStyle){
cssValue = window.getComputedStyle(el,null)[camelCase];
}
return cssValue;
};
}();
var getBackgroundPosition = function (){
var s = getCSS(document.body, 'background-position') === undefined;
return function(el){
if (s){
return getCSS(el, 'background-positionX') + ' ' + getCSS(el, 'background-positionY');
}else{
return getCSS(el, 'background-position');
}
};
}();









评论(1)
看不懂,果然是高人啊。那我转给我计算机专业的朋友看看~~