JQuery——获取元素宽高

1、width() ( height() )

不传参时返回元素的宽度,传参时改变元素的宽度(参数可以为数字,也可以是带像素单位的字符串

<style>
        div{background-color: rebeccapurple;height: 100px;width: 100px;}
    </style>
</head>
<body>
    <div>
        123456
    </div>
</body>
</html>
<script>
    $("div").height("400px")
    $("div").width(300)
</script>

2、innerHeight() (  innerWidth() )

返回元素的height+padding

<style>
        div{background-color: rebeccapurple;height: 100px;width: 100px;padding: 10px;margin: 5px;border: 5px solid forestgreen;}
    </style>
</head>
<body>
    <div>
        123456
    </div>
</body>
</html>
<script>
    console.log($("div").innerHeight(),$("div").innerWidth())//120  120
</script>

3、outerHeight() (  outerWidth() )

不传参时返回border+Height+padding

向方法传入参数true时,返回border+Height+padding+margin

 <style>
        div{background-color: rebeccapurple;height: 100px;width: 100px;padding: 10px;margin: 20px;border: 5px solid forestgreen;}
    </style>
</head>
<body>
    <div>
        123456
    </div>
</body>
</html>
<script>
    console.log($("div").outerHeight(),$("div").outerWidth())         //130 130
    console.log($("div").outerHeight(true),$("div").outerWidth(true))//170 170
</script>

javascript中获取宽高的方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值