jQuery尺寸width() height() innerWidth() innerHeight() outerWidth() outerHeight()Document&Window

本文深入讲解了使用jQuery获取元素尺寸的各种方法,包括width(), height(), innerWidth(), innerHeight(), outerWidth()和outerHeight()等函数的用法及区别,通过实例演示如何获取div元素的不同尺寸。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <script src="jquery.js">
    </script>
    <script>
        $(document).ready(function() {
            $("button").click(function() {
                var txt = "";
                txt += "Width of div: " + $("#div1").width() + "</br>";
                txt += "Height of div: " + $("#div1").height() + "</br>";
                txt += "Inner width of div: " + $("#div1").innerWidth() + "</br>";
                txt += "Inner height of div: " + $("#div1").innerHeight() + "</br>";
                txt += "Outer width of div: " + $("#div1").outerWidth() + "</br>";
                txt += "Outer height of div: " + $("#div1").outerHeight() + "</br>";
                txt += "Outer width of div (margin included): " + $("#div1").outerWidth(true) + "</br>";
                txt += "Outer height of div (margin included): " + $("#div1").outerHeight(true) + "</br>";
                txt += "Document width/height: " + $(document).width();
                txt += "x" + $(document).height() + "</br>";
                txt += "Window width/height: " + $(window).width();
                txt += "x" + $(window).height();
                $("#div1").html(txt);
            });
        });
    </script>
</head>

<body>

    <div id="div1" style="height:200px;width:500px;padding:10px;margin:3px;border:1px solid blue;background-color:lightblue;"></div>
    <br>
    <button>显示 div 的尺寸</button><a>height:200px--width:500px--padding--10px--margin:3px--border:1px</a>
    <p>width() - 返回元素的宽度。</p>
    <p>height() - 返回元素的高度。</p>
    <p>innerWidth() - 返回元素的宽度(包括内边距)。</p>
    <p>innerHeight() - 返回元素的高度(包括内边距)。</p>
    <p>outerWidth() - 返回元素的宽度(包括内边距和边框)。</p>
    <p>outerHeight() - 返回元素的高度(包括内边距和边框)。</p>
    <p>outerWidth(true) - 返回元素的宽度(包括内边距、边框和外边距)。</p>
    <p>outerHeight(true) - 返回元素的高度(包括内边距、边框和外边距)。</p>

</body>

</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值