js 获取width为auto时的实际宽度

要获取div运行时的width,需要用到currentStyle(IE),defaultView(FF) 样式值 

function getStyle( elem, name ) {  
     //如果该属性存在于style[]中,则它最近被设置过(且就是当前的)  
     if (elem.style[name])  
         return elem.style[name];  
     //否则,尝试IE的方式  
     else if (elem.currentStyle)  
         return elem.currentStyle[name];  
     //或者W3C的方法,如果存在的话  
     else if (document.defaultView && document.defaultView.getComputedStyle) {  
         //它使用传统的"text-Align"风格的规则书写方式,而不是"textAlign"  
         name = name.replace(/([A-Z])/g,"-$1");  
         name = name.toLowerCase();  
         //获取style对象并取得属性的值(如果存在的话)  
         var s = document.defaultView.getComputedStyle(elem,"");  
         return s && s.getPropertyValue(name);  
     //否则,就是在使用其它的浏览器  
     } else  
     return null;  
}  

而在IE下如果width设置的事auto,则得到的还是auto

可以通过offsetWidth计算得到,width=offsetWidth - 边框宽度即可;offsetWidth在不设置width属性时也可以拿到的

var widthTemp=getStyle(target,"width");
		if(widthTemp=="auto"||(!widthTemp)){
		   widthTemp=target.offsetWidth-5;
		}



你可能感兴趣的:(js/ajax)