javascript获取隐藏元素(display:none)的高度和宽度的

标签:关于我们    日期:2018-03-21 10:21    录入:汇盛国际平台    浏览:

  

  
  
  
  
//ж?

  
function getType(o){  
var _t;  
return ((_t=typeof(o))=="object" ? o==null && "null" || Object.prototype.toString.call(o).slice(8,-1):_t).toLowerCase();  
}  
//???

  
function getStyle(el, styleName) {  
return el.style[styleName] ? el.style[styleName] : el.currentStyle ? el.currentStyle[styleName] : window.getComputedStyle(el, null)[styleName];  
}  
function getStyleNum(el, styleName) {  
return parseInt(getStyle(el, styleName).replace(/px|pt|em/ig,''));  
}  
function setStyle(el, obj){  
if (getType(obj)=="object") {  
for (s in obj) {  
var cssArrt=s.split("-");  
for (var i=1; i < cssArrt.length; i++) {  
cssArrt[i]=cssArrt[i].replace(cssArrt[i].charAt(0), cssArrt[i].charAt(0).toUpperCase());  
}  
var cssArrtnew=cssArrt.join("");  
el.style[cssArrtnew]=obj[s];  
}  
}  
else  
if (getType(obj)=="string") {  
el.style.cssText=obj;  
}  
}  
function getSize(el) {  
if (getStyle(el, "display") !="none") {  
return { width: el.offsetWidth || getStyleNum(el, "width"), height: el.offsetHeight || getStyleNum(el, "height") };  
}  
var _addCss={ display: "", position: "absolute", visibility: 'hidden' };  
var _oldCss={};  
for (i in _addCss) {  
_oldCss[i]=getStyle(el, i);  
}  
setStyle(el, _addCss);  
var _width=el.clientWidth || getStyleNum(el, "width");  
var _height=el.clientHeight || getStyleNum(el, "height");  
for (i in _oldCss) {  
setStyle(el, _oldCss);  
}  
return { width: _width, height: _height };  
}  
var dd=document.getElementById("test_display_block");  
alert(getSize(dd).height);  
document.getElementById("test_display_click").onclick=function(){  
dd.style.display="block";  
document.getElementById("test_display_none").style.display="block";  
}  
alert($("#test_display_none").height());  
  

★★★小编:汇盛国际注册 整理文章,欢迎大家转载 ★★★