상세 컨텐츠

본문 제목

[j쿼리] 버튼누르면 상자색 바뀌는거

카테고리 없음

by esoesmio 2023. 5. 25. 19:42

본문

<!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>Title</title>
    <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>

    <style>
.box{
    width: 100px;
    height: 100px;

}


    </style>
</head>
<body>
<div class="box"></div>
<button id="btn"></button>
</body>

<script>
const colorarr = ['red','blue','black','gold','green'];
let index = 0;
$("#btn").on('click',()=>{
    $(".box").css("backgroundColor",colorarr[index++]);
    if(index===5){
        index=0;
    }
});
//강제 이벤트 발생

const interval = setInterval(()=>{
    $("#btn").click();

},1000)
</script>
</html>

댓글 영역