본문 바로가기
웹 개발 공부중

버튼(Flip-Flop) 함수 javaScript

by redeyes 2021. 12. 14.

 

버튼 클릭을 토글방식처럼 on/off가능함 응용하면 더욱 다양하게 적용할 수 있음

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script>
        var count=1;
       function hey(){
           alert('버튼');
           count += 1;
           if(count % 2 == 0)
               alert('ON')
           else
               alert('OFF')
       }
    </script>
</head>
<body>
    <button onclick="hey();">버튼</button>
</body>
</html>

여기에 부트스트랩 적용하면 

아래처럼 더 길게 쓸수 있어요. 🤦‍♂️

<!doctype html>
<html lang="ko">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
        crossorigin="anonymous"></script>
        
    <style>
        button {
            margin: 100px;
        }
    </style>
    
    <script>
    var count=1;
       function hey(){
           alert('안녕');
           count += 1;
           if(count % 2 == 0)
               alert('ON')
           else
               alert('OFF')
       }
    </script>

    <title>부트스트랩 연습하기</title>
</head>

<body>
  	<button type="button" class="btn btn-dark">버튼</button>
</body>

</html>

'웹 개발 공부중' 카테고리의 다른 글

ajax 따릉이 데이터가져오기  (0) 2021.12.14
ajax , json , jquery 다루기  (0) 2021.12.14
jQuery 이용해서 div박스 열고 닫기  (0) 2021.12.14
부트스트랩 버튼  (0) 2021.12.14
구글 폰트 적용  (0) 2021.12.14