javascript 未捕获的类型错误:无法读取 null 的属性“clientHeight”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21212114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:23:44 来源:igfitidea点击:
Uncaught TypeError: Cannot read property 'clientHeight' of null
提问by dillonbrannick
I don't know why it can't find the height. Any help would be appreciated.
我不知道为什么它找不到高度。任何帮助,将不胜感激。
var h = document.getElementById('big_button').clientHeight,
center = -h / 2;
window.onload = function () {
document.getElementById('big_button_container2').style.marginTop = 'center';
};
采纳答案by Andrew Robinson
You can use document.getElementsByClass instead as you have only elements with class names not IDs.
您可以改用 document.getElementsByClass,因为您只有具有类名而不是 ID 的元素。
i.e.
IE
var h = document.getElementsByClassName('big_button')[0].clientHeight,
center = -h / 2;
window.onload = function () {
document.getElementsByClassName('big_button_container2')[0].style.marginTop = 'center';
};