Get div height with plain JavaScript

前端 未结 10 905
梦毁少年i
梦毁少年i 2020-11-22 17:22

Any ideas on how to get a div\'s height without using jQuery?

I was searching Stack Overflow for this question and it seems like every answer is pointing to jQuery\'

10条回答
  •  渐次进展
    2020-11-22 17:39

    The other answers weren't working for me. Here's what I found at w3schools, assuming the div has a height and/or width set.

    All you need is height and width to exclude padding.

        var height = document.getElementById('myDiv').style.height;
        var width = document.getElementById('myDiv').style.width;
    

    You downvoters: This answer has helped at least 5 people, judging by the upvotes I've received. If you don't like it, tell me why so I can fix it. That's my biggest pet peeve with downvotes; you rarely tell me why you downvote it.

提交回复
热议问题