[jQuery] width - CSS

<style>
#rect0{
  margin:10px;
  width:100px;
  height:100px;
  display:block;
  padding:8px;
  background-color:#aaa;
}
</style>

$('#btn0').click(function(){
  $('#input0').val( 'document width : ' + $(document).width() + 'px' );
  $('#input1').val( 'window width : ' + $(window).width() + 'px' );
  $('#input2').val( 'div width : ' + $('#rect0').width() + 'px' );
  $('#input3').val( 'p width : ' + $('#rect0 p').width() + 'px' );
});

.width() return number
指定した要素の幅を取得する

id = rect0

幅を取得する

<style>
#rect1{
  margin:10px;
  width:100px;
  height:100px;
  display:block;
  padding:8px;
  background-color:#aaa;
}
</style>

$('#btn1').click(function(){
  $('#rect1').width(300);
  $('#input4').val( 'div width : ' + $('#rect1').width() + 'px' );
});

.width( value:number )
指定した要素の幅を設定する

id = rect1

幅を設定する

<style>
#rect2{
  margin:10px;
  width:100px;
  height:100px;
  display:block;
  padding:8px;
  background-color:#aaa;
}
</style>

$('#btn2').click(function(){
  $('#rect2').width( function(index,value){
    return value + 100;
  } );
  $('#input5').val( 'div width : ' + $('#rect2').width() + 'px' );
});

.width( function( index:int, value:number ):function )
指定した要素の幅を設定する

id = rect2

幅を設定する