2016年2月1日月曜日

(jQueryの学習)CSS:高さを操作する/innerHeightメソッド/borderの内側の高さを取得する

CSSの高さを操作する方法の2回目です。

本日は「2.borderの内側の高さ(borderは除き、paddingは含む)を取得する」を学びます。

2.borderの内側の高さ(borderは除き、paddingは含む)を取得する

2-1.innerHeightメソッド【返却値(数値) = 要素.innerHeight( )】でborderの内側の高さを取得する
次にinnerHeightを取得します。

innerHeightとは「borderの内側の高さ」を表します。
つまり、「上下のpadding+height=innerHeight」です。

利用方法はheightメソッドと同様。

注意:このメソッドは設定では使用できません。
サンプルソース
// HTML
<input type="button" value="ボタンを押すと、指定した要素の高さが表示されます"><br>
<div class="box">
  <div class="box"><b>テストエリア1</b><div class="box-css-test-area-1">・・・略・・・</div></div>
</div>

// CSS
/* テストエリア1 */
.box-css-test-area-1 {background:#F5EEFF; border:5px solid #88f; padding:10px; margin:20px; height:40px; width:460px;}

// jQuery
$(function() {
  $("input[type=button]").click(function(){
    // 【構文】返却値(数値) = 要素.innerHeight()
    //   サンプル:「テストエリア1」のborderの内側の高さを取得する
    alert("[" + $(".box-css-test-area-1").innerHeight() + "]");
  });
});
実装例
指定した要素に設定されているborderの内側の高さを取得します。

要素:

テストエリア1
border:5px; padding:10px; margin:20px; height:40px; width:460px;
(境界線:5px、枠内余白:10px、枠外余白:20px、高さ:40px、幅:460px)
テストエリア2
border:5px; padding:20px; margin:10px; height:30px; width:450px;
(境界線:5px、枠内余白:20px、枠外余白:10px、高さ:30px、幅:450px)

いかがでしたか?
次回は「3.borderの外側の高さ(border、paddingを含む)を取得する」を学びます。

それでは、また。

Previous Post
Next Post

post written by: