gpt4 book ai didi

javascript - 没有 jquery 的 outerWidth

转载 作者:IT王子 更新时间:2023-10-29 03:14:42 26 4
gpt4 key购买 nike

是否可以使用dom获取元素outerWidth?

Ej:

var width = document.getElementById('myDiv').outerWidth; 

最佳答案

没有,但你可以获得 offsetWidth,这可能是你想要的。

来自 http://www.quirksmode.org/dom/w3c_cssom.html

offsetWidthoffsetHeight

The width & height of the entire element, including borders and padding (excluding margins).

clientWidthclientHeight

The width & height of the element including padding (excluding borders and margins)

参见 this fiddle举个例子。

var elm = document.querySelector('div');

document.body.innerHTML += `
clientWidth: ${elm.clientWidth} px
<br>
scrollWidth: ${elm.scrollWidth} px
<br>
offsetWidth: ${elm.offsetWidth} px`

div{
width : 200px;
padding : 10px;
border : 10px solid gold;

margin : 10px;
background : lightgreen;
}
<div></div>

如果您使用 jQuery,您有更多选择:width、innerWidth 和 outerWidth 属性。 http://api.jquery.com/category/manipulation/style-properties/

关于javascript - 没有 jquery 的 outerWidth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5195402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com