使用Javascript或HTML,如何获取div或其他元素的高度和宽度? [英] Using Javascript or HTML, how to get the height and width of a div or other element?

查看:174
本文介绍了使用Javascript或HTML,如何获取div或其他元素的高度和宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了一段时间来获取我的网页上的div的高度和宽度。我试过很多东西,有些是:

I've been trying for a while now to 'get' the height and width of a div on my webpage. I've tried many things, some are:

document.getElementById("header").getHeight();
                                 .height();
                                 .height;
                                 .pixelHeight()

和所有其他'明显'任何帮助吗?它只是一个宽度和高度的简单div:

and all other 'obvious' possibilites. Any help please? Its just a simple div with a width and a height:

<style>
    #header {
        width: 500px; 
        height:100px; 
        background-color:black; 
        border: 2px solid red;
    }
</style>
<body>
    <div id="header" style="position:absolute" onclick="testjs()" />
    <script language="javascript" type="text/javascript">
        var hdiv = document.getElementById("header");

        function testjs()
        {
            hdiv.style.height="221px";
        }           

        /////////////////////////////////////
        hdiv.getHeight();              ???????
        //////////////////////////////////////
    </script>

我想要获取div的高度和宽度,因为最终用户将能够改变高度和宽度动态。

I want to 'get' the div's height and width because eventually the user will be able to alter the height and width dynamically.

推荐答案

获取高度使用

document.getElementById("header").offsetHeight



and for width use

document.getElementById("header").offsetWidth;

这篇关于使用Javascript或HTML,如何获取div或其他元素的高度和宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆