javascript - How to get the height of a height-adaptive element?
phpcn_u1582
phpcn_u1582 2017-05-16 13:43:33
0
5
501

The height of my element box is not set and is expanded by the subset content. How to get its height? All methods such as height are 0. Is there any way to get the actual height?

phpcn_u1582
phpcn_u1582

reply all(5)
習慣沉默

You can delay for a while before getting the height of the content, for example like this

        const oBox=document.getElementById(id);
        setTimeout(function(){
            console.log(oBox.scrollHeight)
        },500)
大家讲道理

clientHeight, scrollHeight, offsetHeight
innerHeight, outerHeight
Basically there is always one that suits you

某草草

The values ​​obtained are all 0 because the height is not set. What I want is the actual height displayed after the web page is loaded, including subsets

给我你的怀抱

The first possibility is that js is executed before rendering is completed. . Obtaining 0
The second possibility is that the child elements are all floating or absolutely positioned. The outermost layer has no height
The third possibility is that the wrong element is obtained, is the js written wrong?

刘奇

It should be caused by executing the code to obtain the height before the element is rendered.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!