• 코드:
​x
 
1
<!DOCTYPE html>
2
<html lang="ko">
3
​
4
<head>
5
    <meta charset="UTF-8">
6
    <title>jQuery Element Access</title>
7
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
8
    <script>
9
        $(function() {
10
            $("#getter").on("click", function() {
11
                var size = "너비는 " + $("#box").width() + "px이고, 높이는 "
12
                    + $("#box").height() + "px입니다.<br>";
13
                $("#text").html(size);
14
            });
15
            $("#setter").on("click", function() {
16
                w = $("#box").width();
17
                h = $("#box").height();
18
                $("#box").width(w/2).height(h/2);
19
​
20
                var size = "너비는 " + $("#box").width() + "px이고, 높이는 "
21
                    + $("#box").height() + "px로 변경되었습니다.<br>";
22
                $("#text").html(size);
23
            });
24
        });
25
    </script>
26
</head>
27
​
28
<body>
29
​
30
    <h1>.width() 메소드와 .height() 메소드</h1>
31
    <p>아래의 버튼을 누르면 다음 div 요소의 크기를 읽어오거나 설정할 수 있어요!!</p>
32
    <button id="getter">크기 읽어오기!</button>
33
    <button id="setter">크기 줄이기!</button><br><br>
34
    <div id="box" style="width: 400px; height: 200px; background-color: yellow"></div>
35
    <p id="text"></p>
36
    
37
</body>
38
​
39
</html>