본문 바로가기
JavaScript/실습

[javascript] function의 파라미터, 이벤트리스너

by Angry Stock 2023. 1. 7.
반응형

※ 다른 버튼을 누르면 알림 창의 다른 내용 보이게 하기 

※ 하나의 function으로 다른 역할을 하는 버튼 만들기

닫기 버튼은 이벤트리스너로 구현

 

1. html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link href="css/reset.css" rel="stylesheet">
    <link href="css/main.css" rel="stylesheet">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>
    <div class="alert-box" id="alert">
        <span class="alerttext">알림창임</span>
        <button class="alertclose-btn">X</button>
    </div>
    <button onclick="openbtn('block','아이디입력하세요');">버튼1</button>
    <button onclick="openbtn('block','비번입력하세요');">버튼2</button>

    <script>
        document.getElementsByClassName('alertclose-btn')[0].addEventListener('click', function() {
            document.getElementById('alert').style.display = 'none';
        });


        function openbtn(a, b) {
            document.getElementById('alert').style.display = a;
            document.getElementsByClassName('alerttext')[0].innerHTML = b;
        };
    </script>

</body></html>

2. main.css

.alert-box {
    background-color: skyblue;
    padding: 20px;
    color: white;
    border-radius: 10px;
    display: none;
    float: left;
    width: 100%;
}

.alertclose-btn {
    font-size: 16px;
    float: right;
    background-color: skyblue;
    border: none;
    color: white;
    height: 21px;
}

.alerttext {
    float: left;
    vertical-align: 
}

3. reset.css

body {
    margin: 0
}

div {
    box-sizing: border-box;
}

4. 출력화면

반응형

댓글