最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: Cannot read property 'clientHeight' of null - Stack Overflow

programmeradmin1浏览0评论

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

/

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

http://jsfiddle/4zux6/

Share Improve this question asked Jan 19, 2014 at 1:25 dillonbrannickdillonbrannick 3221 gold badge5 silver badges17 bronze badges 4
  • 1 You have no element with the ID big_button. – cookie monster Commented Jan 19, 2014 at 1:27
  • Needs semicolon ; after clientHeight instead of ma , ? – John McMahon Commented Jan 19, 2014 at 1:28
  • @JohnMcMahon: The , is correct. It's declaring multiple variables. – cookie monster Commented Jan 19, 2014 at 1:28
  • @cookiemonster can't believe I missed that, I must be quite tired – dillonbrannick Commented Jan 19, 2014 at 1:42
Add a ment  | 

1 Answer 1

Reset to default 2

You can use document.getElementsByClass instead as you have only elements with class names not IDs.

i.e.

var h = document.getElementsByClassName('big_button')[0].clientHeight,
    center = -h / 2;
window.onload = function () {
    document.getElementsByClassName('big_button_container2')[0].style.marginTop = 'center';
};
发布评论

评论列表(0)

  1. 暂无评论