jQuery 너비() 메서드

❮ jQuery HTML/CSS 메소드

예시

<div> 요소의 너비를 반환합니다.

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

정의 및 사용

width() 메서드는 선택한 요소의 너비를 설정하거나 반환합니다.

이 메서드를 사용하여 너비를 반환하면 첫 번째로 일치하는 요소의 너비를 반환합니다 .

이 방법을 사용하여 너비를 설정 하면 일치하는 모든 요소의 너비를 설정합니다.

아래 이미지에서 볼 수 있듯이 이 메서드에는 패딩, 테두리 또는 여백이 포함되지 않습니다.

jQuery 차원


관련 방법:

  • height() - 요소의 높이를 설정하거나 반환합니다.
  • innerWidth() - 요소의 너비를 반환합니다(패딩 포함).
  • innerHeight() - 요소의 높이를 반환합니다(패딩 포함).
  • outerWidth() - 요소의 너비를 반환합니다(패딩 및 테두리 포함).
  • outerHeight() - 요소의 높이를 반환합니다(패딩 및 테두리 포함).


통사론

너비 반환:

$(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() 사용 방법.


작은 화면에서 페이지의 배경색을 변경하는 방법.


❮ jQuery HTML/CSS 메소드