admin 管理员组

文章数量: 1086019

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

本文标签: javascriptUncaught TypeError Cannot read property 39clientHeight39 of nullStack Overflow