Get div height with plain JavaScript

前端 未结 10 826
梦毁少年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:53

    try

    myDiv.offsetHeight 
    

    console.log("Height:", myDiv.offsetHeight );
    #myDiv { width: 100px; height: 666px; background: red}
    <div id="myDiv"></div>

    0 讨论(0)
  • 2020-11-22 17:53
    <div id="item">show taille height</div>
    <script>
        alert(document.getElementById('item').offsetHeight);
    </script>
    

    Jsfiddle

    0 讨论(0)
  • 2020-11-22 17:58

    Here's one more alternative:

    var classElements = document.getElementsByClassName("className");
    
    function setClassHeight (classElements, desiredHeightValue) 
        {
            var arrayElements = Object.entries(classElements);
            for(var i = 0; i< arrayElements.length; i++) {
                arrayElements[i][1].style.height = desiredHeightValue;
            }
    
        }
    
    0 讨论(0)
  • 2020-11-22 18:01

    jsFiddle

    var element = document.getElementById('element');
    alert(element.offsetHeight);
    
    0 讨论(0)
提交回复
热议问题