HOME > > > > >

.height()

.height()

戻り値

Integer
数値

jQueryオブジェクトが持つDOM要素の最初のDOM要素の高さを返す。margin、padding、borderは含まない。

サンプル

実行結果


ここの
高さは?

Javascript

  1. var str = 'window : ' + $(window).height() + '<br />';
  2. str += 'document : ' + $(document).height() + '<br />';
  3. str += 'jqTest : ' + $('.jqTest').height() + '<br />';
  4. str += 'No Element : ' + $('.NoElement').height();
  5. $('.jqTest .screen').append(str);
  6.  
  7. $('.jqTest .targetBox').css({
  8.         padding: 20,
  9.         margin: 20,
  10.         border: '20px solid #990000',
  11.         background: '#CCCCCC'
  12. }).click(function(){
  13.         alert('height:' + $(this).height());
  14. });

HTML

  1. <div class="jqTest">
  2.     <div class="screen"></div>
  3.     <hr />
  4.     <div class="targetBox">
  5.     ここの<br />
  6.     高さは?
  7.     </div>
  8. </div>

.height( value )

引数

val String, Number
単位付きの文字列もしくは数値

戻り値

Integer
数値

要素を指定の高さに設定する。

サンプル

実行結果

  • 50px
  • 20em
  • 200%

Javascript

  1. var num = '';
  2.  
  3. $('.jqTest2 li')
  4. .css('border', '1px solid #000000')
  5. .each(function(i){
  6.     $(this).mouseover(function(){
  7.         num = $(this).text();
  8.         $(this).height(num);
  9.     });
  10. });

HTML

  1. <div class="jqTest2">
  2.     <ul>
  3.     <li>50px</li>
  4.     <li>20em</li>
  5.     <li>200%</li>
  6.     </ul>
  7. </div>

ん。%が動かない。

.height( function(index, height) )

引数

(int)index
jQueryオブジェクト内でのindex
(int)height
ターゲットの要素の高さ

heightの値となるものをreturnするとheightの値として設定してくれる。

サンプル

実行結果

  • 50px
  • 100px
  • 150px

Javascript

  1. var num = '';
  2.  
  3. $('.jqTest3 li').each(function(i){
  4.     $(this).css({
  5.         display: 'inline-block',
  6.         height: 80,
  7.         width: 80,
  8.         background: '#2AA198'
  9.     });
  10. });
  11.  
  12. $('.jqTest3 .execute').toggle(function(){
  13.     $('.jqTest3 li').each(function(){
  14.         $(this).height(function(index, height){
  15.                 return '+=' + parseInt($(this).text());
  16.         });
  17.     });
  18. }, function(){
  19.     $('.jqTest3 li').each(function(){
  20.         $(this).height(function(index, height){
  21.                 return '-=' + parseInt($(this).text()) / 2;
  22.         });
  23.     });
  24. });

HTML

  1. <div class="jqTest3">
  2.         <input type="button" value="execute" class="execute" />
  3.     <ul>
  4.     <li>50px</li>
  5.     <li>100px</li>
  6.     <li>150px</li>
  7.     </ul>
  8. </div>