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
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.