簡體   English   中英

我可以將offsetHeight分配給style.top嗎?

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

在javascript中,我可以這樣分配嗎:

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

運行上述腳本后,div的“內容”是否具有“頂部”屬性= tempHeight? 我試過了,但是不行,有什么建議嗎? 謝謝!

你只需要一個單位

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

top CSS屬性需要一個單位和值。 添加px應該可以:

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

您必須在style.top語句的末尾添加+ 'px'或某個單位。

另外,# #content必須設置了position樣式

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';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM