jQueryオブジェクトが持つDOM要素の最初のDOM要素の幅を返す。paddingは含むがborderは含まれない。
windowやdocumentオブジェクトを対象にする場合は.width()を利用する。
サンプル
Javascript
var str = 'window : ' + $(window).innerWidth() + '/' + $(window).outerWidth() + '<br />';
str += 'document : ' + $(document).innerWidth() + '/' + $(document).outerWidth() + '<br />';
str += 'jqTest : ' + $('.jqTest').innerWidth() + '/' + $('.jqTest').outerWidth() + '<br />';
str += 'No Element : ' + $('.NoElement').innerWidth() + '/' + $('.NoElement').outerWidth();
$('.jqTest .screen').append(str);
$('.jqTest .targetBox').css({
padding: 20,
margin: 20,
border: '20px solid #990000',
background: '#CCCCCC'
}).click(function(){
alert('[outerWidth]' + $(this).outerWidth() + '[innerWidth]' + $(this).innerWidth() + '[width]' + $(this).width());
});
HTML
<div class="jqTest">
<div class="screen"></div>
<hr />
<div class="targetBox">
ここの<br />
幅は?
</div>
</div>
.outerWidth( [includeMargin] )
引数
- (boolean)includeMargin
- デフォルトはfalse。trueでmarginも含めた幅を算出する
サンプル
Javascript
var str = 'window : ' + $(window).innerWidth() + '/' + $(window).outerWidth(true) + '<br />';
str += 'document : ' + $(document).innerWidth() + '/' + $(document).outerWidth(true) + '<br />';
str += 'jqTest : ' + $('.jqTest').innerWidth() + '/' + $('.jqTest').outerWidth(true) + '<br />';
str += 'No Element : ' + $('.NoElement').innerWidth() + '/' + $('.NoElement').outerWidth(true);
$('.jqTest2 .screen').append(str);
$('.jqTest2 .targetBox').css({
padding: 20,
margin: 20,
border: '20px solid #990000',
background: '#CCCCCC'
}).click(function(){
alert('[outerWidth]' + $(this).outerWidth(true) + '[innerWidth]' + $(this).innerWidth() + '[height]' + $(this).height());
});
HTML
<div class="jqTest2">
<div class="screen"></div>
<hr />
<div class="targetBox">
ここの<br />
高さは?
</div>
</div>