How does Javascript get and set the height and width of the div and be compatible with any browser? Check the Code:
Copy codeThe Code is as follows:
<Div id = "div1" style = "height: 300px; width: 200px;"> http://www.itdos.com </div>
<Div id = "div2" style = "height: 30px; width: 20px;"> http://www.itdos.com </div>
Obtain the div1 width and height:
Copy codeThe Code is as follows:
Alert (document. getElementById ("div1"). offsetHeight); // compatible with FF, IE, etc.
Alert (document. getElementById ("div1"). offsetWidth); // compatible with FF, IE, etc.
Set div1 to div2:
Copy codeThe Code is as follows:
Document. getElementById ("div1"). style. height = document. getElementById ("div2"). offsetHeight; // only IE
Document. getElementById ("div1"). style. height = document. getElementById ("div2"). offsetHeight + "px"; // compatible with FF, IE, etc.
Document. getElementById ("div1"). style. width = document. getElementById ("div2"). offsetWidth; // only IE
Document. getElementById ("div1"). style. width = document. getElementById ("div2"). offsetWidth + "px"; // compatible with FF, IE, etc.