JavaScript

[JavaScript] jQuery (버튼 클릭 시 크기, 색상 변경)

퓨어맨 2022. 5. 31. 09:14
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1{
            width: 100px;
            height: 100px;
            background-color: royalblue;
            font-size: 30px;
            text-align: center;
        }

        .box2{
            width: 200px;
            height: 200px;
            background-color: black;
        }
    </style>
</head>
<body>
    <div class = 'box1'>
        클릭
    </div>

    <script src="js/jquery-3.6.0.min.js"></script>
    <script>
        // 이벤트 처리
        $('div').on('click',()=>{
            // div 태그의 너비 200px, 높이 200px, 배경색상은 검정으로 변경
            // $('.box1').css({width:'200px', height:'200px',
            // backgroundColor : 'black'})
            $('div').removeClass().addClass('box2')
        })


    </script>

</body>
</html>

클릭 전

클릭 후