为什么不this代码工作?我正在尝试(不使用 JQuery)获取 html 元素的渲染高度。下面这段代码:
html:
<body id="logo">
</body>
CSS:
body {
background-image: url('https://www.gstatic.com/images/branding/googlelogo/2x/googlelogo_color_284x96dp.png');
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100vw auto;
background-position: -80px 50%;
}
JS:
var element = document.getElementById(logo).offsetHeight;
console.log(element);
但是这段代码给出了这个错误:
VM227:51 Uncaught TypeError: Cannot read property 'offsetHeight' of null
我做错了什么?
最佳答案
你的代码应该更像这样:
document.getElementById('logo')
否则你试图将带有 id 的元素放入 var 标识中。
问候
关于javascript - 尝试在纯 JS 中获取元素的渲染高度时元素为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36500692/