jQuery width()メソッド

❮jQueryHTML/ CSSメソッド

<div>要素の幅を返します。

$("button").click(function(){
    alert($("div").width());
}); 

定義と使用法

width()メソッドは、選択した要素の幅を設定または返します。

このメソッドを使用して幅を返す場合、最初に一致した要素の幅を返します

このメソッドを使用して幅を設定すると、一致するすべての要素の幅が設定されます。

下の画像が示すように、この方法にはパディング、ボーダー、マージンは含まれていません。

jQueryディメンション


関連する方法:

  • height() -要素の高さを設定または返します
  • innerWidth() -要素の幅を返します(パディングを含む)
  • innerHeight() -要素の高さを返します(パディングを含む)
  • externalWidth() -要素の幅を返します(パディングと境界線を含みます)。
  • externalHeight() -要素の高さを返します(パディングと境界線を含みます)。


構文

幅を返します:

$(selector).width()

幅を設定します。

$(selector).width(value)

関数を使用して幅を設定します。

$(selector).width(function(index,currentwidth))

Parameter Description
value Required for setting width. Specifies the width in px, em, pt, etc. Default unit is px
function(index,currentwidth) Optional. Specifies a function that returns the new width of selected elements
  • index - Returns the index position of the element in the set
  • currentwidth - Returns the current width of the selected element

自分で試してみてください-例


の幅を設定するさまざまな単位を使用して要素の幅を設定する方法。


を使用して幅を減らす関数を使用して要素の幅を減らす方法。


の現在の幅を返す方法。


するwidth()、height()、innerHeight()、innerWidth()、outerWidth()、outerHeight()の使用方法。


小さい画面でページの背景色を変更する方法。


❮jQueryHTML/ CSSメソッド