简体   繁体   English

我可以将offsetHeight分配给style.top吗?

[英]Can I assign offsetHeight to style.top?

In javascript, can I assign like this: 在javascript中,我可以这样分配吗:

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

After running above script, will the div "content" have "top" propery = tempHeight? 运行上述脚本后,div的“内容”是否具有“顶部”属性= 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. top CSS属性需要一个单位和值。 Adding px should work: 添加px应该可以:

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

You have to add + 'px' or some unit to the end of the style.top statement. 您必须在style.top语句的末尾添加+ 'px'或某个单位。

Also, #content must have the position style set 另外,# #content必须设置了position样式

http://jsfiddle.net/BshuC/ http://jsfiddle.net/BshuC/

CSS CSS

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

JS JS

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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