【问题标题】:Why does Client Height value doesn't apply to other elements? [duplicate]为什么客户端高度值不适用于其他元素? [复制]
【发布时间】:2021-09-05 20:43:36
【问题描述】:

我希望我的侧边栏与顶部的间距等于标题的高度。当我使用 clientHeight在 js 中,虽然当我通过 alert() 打印它时它返回正确的值,但它不适用于 marginToptop 值等其他元素。 (Position 包含在使用 top 时)。

HTML

<body onload="sidenav_height()">
 ...
</body>

JS

function sidenav_height(){
  var height = document.getElementById("header").clientHeight;
  document.getElementById("sidenav").style.marginTop = height;
}

【问题讨论】:

  • style.marginTop的值需要一个单位,.clientHeight只返回一个数字。

标签: javascript html css web


【解决方案1】:

请更改

function sidenav_height(){
  var height = document.getElementById("header").clientHeight;
  document.getElementById("sidenav").style.marginTop = height+"px";
}

【讨论】:

    猜你喜欢
    • 2019-12-24
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    • 2011-12-06
    • 2015-10-22
    相关资源
    最近更新 更多