OffsetHeight is obtained as 0 in OnLoad.

Source: Internet
Author: User

In IE, especially in the current div + css method, the height of the div is usually not defined. This is usually used when you need to obtain the height of the div after adding the div content.

In use, sometimes the offsetHeight value is 0, but if you use various JS debugging tools for debugging, you can also see the value in the object (if the direct point to offsetHeight is not a value, but if it is an object that has a value, press enter in the debugger to view the object and the object has been refreshed, so there is a value .)

For example
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html>
<Head>
<Script language = 'javascript '>
Window. attachEvent ("onload", function () {_ resizeScroll2 ();});
Window. onresize = function () {winresize ();};
Function _ resizeScroll2 (){
Var html1 = '<div id = "divcj" style = "margin-top: 15px; font-size: 10px; width: 400px;">'
+ '<Div style = "float: left; width: 50px;"> test </div>'
+ '<Div style = "float: left; width: 320px;"> danielinbiti </div>'
+ '</Div>'
+ '<Div id = "divcj2" style = "margin-top: 15px; font-size: 10px; width: 400px;">'
+ '<Div style = "width: 320px;"> danielinbiti </div>'
+ '</Div>'
Document. getElementById ('outer'). innerHTML = html1;
Document. getElementById ('divcj1'). style. display = 'none ';
Alert (document. getElementById ('divcj1'). offsetHeight );
}
</Script>
</Head>
<Body>
<Div id = 'outer'> </div>
</Body>
</Html>

If the divcj height is obtained, the value 0 is obtained in onload. Because divcj has a float layout.
In this case, if the div is simple, you can use the hidden layer, such as divcj2, to remove the float and obtain the same height as divcj.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.