A hidden DOM cannot be obtained in width. If you want to obtain it, use the following method:
First, clone a DOM, set position: absolute, set top to a relatively large negative value, and display it. Finally, retrieve the width and height of the DOM and remove it.
The Code is as follows:
Copy to ClipboardReference: [www.bkjia.com] 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.css Text = _ ostyle;
DomA. setAttribute ("href", "javascript: void (0 );");
Document. getElementsByTagName ('body') [0]. appendChild (o );
Console. log (getWH (domA ));
There are other better methods.
Source: http://www.js8.in/