![](/img/trans.png)
[英]How do I set the style.top property of an element in JavaScript?
[英]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
樣式
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.