First clone a DOM, set Position:absolute, and then set top to a larger negative value, then show it, and finally get the DOM's width, and remove it.
The specific code is as follows:
JS Code
Copy Code code as follows:
function Getcss (Elem, CSS) {
if (window.getComputedStyle) {
Return window.getComputedStyle (Elem, NULL) [CSS];
}else if (elem.currentstyle) {
return ELEM.CURRENTSTYLE[CSS];
}else {
return ELEM.STYLE[CSS];
}
}
function Getwh (DOM) {
var get = function (elem) {
var wh = {};
' Width Height '. Replace (/[^]+/g, function (i) {
var a = I.tolowercase ();
Wh[a] = elem[' offset ' + i] | | elem[' client ' + i];
});
return WH;
};
if (GETCSS (DOM, ' display ') = = = ' None ') {
var ndom = Dom.clonenode (true);
nDom.style.position = ' absolute ';
NDom.style.top = ' -3000px ';
NDom.style.display = ' block ';
document.getElementsByTagName (' body ') [0].appendchild (Ndom);
var wh = get (ndom);
NDom.parentNode.removeChild (Ndom);
return WH;
}
Return get (DOM);
}
Test
Console.log (Getwh) (document.getElementById (' content '));
var DomA = document.createelement ("a"), _ostyle = "Position:absolute;z-index:999999;width:92px;height:22px;position: Absolute;display:none; ";
Doma.setattribute ("style", _ostyle);
DomA.style.cssText = _ostyle;
Doma.setattribute ("href", "javascript:void (0);");
document.getElementsByTagName (' body ') [0].appendchild (O);
Console.log (Getwh (DomA));
function Getcss (Elem, CSS) {
if (window.getComputedStyle) {
Return window.getComputedStyle (Elem, NULL) [CSS];
}else if (elem.currentstyle) {
return ELEM.CURRENTSTYLE[CSS];
}else {
return ELEM.STYLE[CSS];
}
}
function Getwh (DOM) {
var get = function (elem) {
var wh = {};
' Width Height '. Replace (/[^]+/g, function (i) {
var a = I.tolowercase ();
Wh[a] = elem[' offset ' + i] | | elem[' client ' + i];
});
return WH;
};
if (GETCSS (DOM, ' display ') = = = ' None ') {
var ndom = Dom.clonenode (true);
nDom.style.position = ' absolute ';
NDom.style.top = ' -3000px ';
NDom.style.display = ' block ';
document.getElementsByTagName (' body ') [0].appendchild (Ndom);
var wh = get (ndom);
NDom.parentNode.removeChild (Ndom);
return WH;
}
Return get (DOM);
}
Test
Console.log (Getwh) (document.getElementById (' content '));
var DomA = document.createelement ("a"), _ostyle = "Position:absolute;z-index:999999;width:92px;height:22px;position: Absolute;display:none; ";
Doma.setattribute ("style", _ostyle);
DomA.style.cssText = _ostyle;
Doma.setattribute ("href", "javascript:void (0);");
document.getElementsByTagName (' body ') [0].appendchild (O);
Console.log (Getwh (DomA));
There are other better ways to welcome this.