.width()
원문 링크
http://api.jquery.com/width/
함수들
width( )
- .width()
width( value )
- .width( value )
- .width( function(index, width) )
개요 : 일치된 요소 집합 중 첫번째 요소에 대해 현재의 계산된 넓이를 얻을 수 있습니다.
- .width()
.css('width') 와 .width() 간의 차이점은 픽셀 텍스트의 유무에 있습니다. 예를 들어, 400 와 400px의 차이입니다. .width() 함수는 수학적인 계산을 필요로 할때 사용하시면 됩니다.

또한 이 함수는 window와 document의 넓이를 알고 싶을 때 사용할 수도 있습니다.
$(window).width(); // returns width of browser viewport $(document).width(); // returns width of HTML document
Note. .width() 함수는 컨텐츠의 넓이만 반환합니다. CSS의 box-sizing 속성값과는 상관없습니다.
예 제
다양한 넓이를 구해봅니다. Note 아이프레임의 넓이는 기대한 것보다 작게 나옵니다. 노란색 강조 영역은 iframe의 body부분입니다.
<!DOCTYPE html>
<html>
<head>
<style>
body { background:yellow; }
button { font-size:12px; margin:2px; }
p { width:150px; border:1px red solid; }
div { color:red; font-weight:bold; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<button id="getp">Get Paragraph Width</button>
<button id="getd">Get Document Width</button>
<button id="getw">Get Window Width</button>
<div> </div>
<p>
Sample paragraph to test width
</p>
<script>
function showWidth(ele, w) {
$("div").text("The width for the " + ele +
" is " + w + "px.");
}
$("#getp").click(function () {
showWidth("paragraph", $("p").width());
});
$("#getd").click(function () {
showWidth("document", $(document).width());
});
$("#getw").click(function () {
showWidth("window", $(window).width());
});
</script>
</body>
</html>미리보기
버튼들을 클릭해 보세요. 그에 맞는 넓이를 반환해 줍니다.
개요 : CSS 넓이를 바꿔줍니다.
- .width( value )
- value 픽셀값
- .width( function(index, width) )
- function(index, width) 높이를 반환하는 함수.
.width(value) 함수의 인자인 value 값으로는 문자열(단위를 포함한 숫자) 또는 숫자를 사용할 수 있습니다. 만일 숫자로 인자를 제공하면 jQuery는 pixel 단위라고 가정합니다. 하지만 문자열을 사용하면 반드시 CSS 에 사용할 수 있는 값만 사용할 수 있습니다. 즉, 100px, 50%, auto와 같이 사용해야 하는 것입니다. Note 최신 브라우져들은 CSS width 속성에 padding, border, margin 값을 포함하지 않습니다.
만약 단위를 명확하게 표현하지 않았다면 "px" 을 연결합니다.
Note .width(value)는 CSS의 box-sizing 속성을 고려하여 width 값을 변경시킵니다. border-box 속성을 변경하면 내용의 높이 대신 박스의 외부높이(outerHeight)를 변경하게 됩니다.
예 제
각 div 영역을 클릭하면 넓이와 색깔을 변화시킵니다.
<!DOCTYPE html>
<html>
<head>
<style>
div { width:70px; height:50px; float:left; margin:5px;
background:red; cursor:pointer; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div></div>
<div>d</div>
<div>d</div>
<div>d</div>
<div>d</div>
<script>
$("div").one('click', function () {
$(this).width(30)
.css({cursor:"auto", "background-color":"blue"});
});
</script>
</body>
</html>
미리보기
클릭해 보세요. 넓이가 줄어듭니다.
그럼 즐프하세요.
※ 본 예제는 http://www.jquery.com 에 있는 내용임을 밝힙니다.
'프로그래밍 > jQuery' 카테고리의 다른 글
| jQuery API 정복 - 변경 이벤트, change() (0) | 2011.11.24 |
|---|---|
| jQuery API 정복 - 포커스 잃을 때 이벤트, blur() (1) | 2011.11.24 |
| jQuery API 정복 - 이벤트 연결하기, bind() (6) | 2011.11.23 |
| jQuery API 정복 - 요소 별로 감싸기, wrapAll() (0) | 2011.11.22 |
| jQuery API 정복 - 요소 감싸기, wrap() (0) | 2011.11.22 |
| jQuery API 정복 - 감싼 요소 제거하기, unwrap() (0) | 2011.11.22 |
| jQuery API 정복 - class 토글하기, toggleClass() (0) | 2011.11.22 |
| jQuery API 정복 - 텍스트만 알아내기, text() (5) | 2011.11.22 |