简体   繁体   中英

Can I assign offsetHeight to style.top?

In javascript, can I assign like this:

var tempHeight = document.getElementById("header").offsetHeight;
document.getElementById("content").style.top = tempHeight;

After running above script, will the div "content" have "top" propery = tempHeight? I tried but it does not work, any suggestions? Thanks!

你只需要一个单位

document.getElementById("content").style.top = String(tempHeight) + "px";

The top CSS property expects a unit along with the value. Adding px should work:

document.getElementById("content").style.top = tempHeight + "px";

You have to add + 'px' or some unit to the end of the style.top statement.

Also, #content must have the position style set

http://jsfiddle.net/BshuC/

CSS

 #header {
        height: 200px;
 }
 #content { position: absolute; }

JS

var tempHeight = document.getElementById("header").offsetHeight;
document.getElementById("content").style.top = tempHeight + 'px';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM