Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

javascript - outerWidth without jquery

It's possible to get the element outerWidth using the dom?

Ej:

var width = document.getElementById('myDiv').outerWidth; 
question from:https://stackoverflow.com/questions/5195402/outerwidth-without-jquery

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No, but you can get offsetWidth, which is probably what you want.

From http://www.quirksmode.org/dom/w3c_cssom.html

offsetWidth and offsetHeight

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

clientWidth and clientHeight

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

See this fiddle for an example.

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...