[JS] change / focus, blur / mouse 이벤트

형이·2023년 8월 16일
0

JavaScript

목록 보기
20/20
post-thumbnail

📝 JavaScript

🖥️ 1. change 이벤트

  • change 이벤트는 요소 변경이 끝나면 발생한다.
  • 텍스트 입력 요소인 경우, 요소 변경이 끝날 때가 아니라 포커스를 잃을 때 이벤트가 발생한다. (버튼을 클릭하는 등의 동작을 통해 포커스를 다른 곳으로 옮기는 순간 change 이벤트가 발생)
EX)

<body>
    <p id="result"></p>
    <input type="text" id="target" />
    <script>
        let t = document.getElementById('target');
        t.addEventListener('change',function(event){
            document.getElementById('result').innerHTML
                        = event.target.value;
        });
    </script>
</body>

🖥️ 2. focus, blur 이벤트

  • focus 이벤트는 요소가 포커스를 받을 때 발생하는 것을 의미한다.
  • blur 이벤트포커스를 잃을 때 발생하는 것을 의미한다.
EX)

<body>
    <input id="target" type="text" />
    <script>
        let t = document.getElementById('target');
        t.addEventListener('blur',function(){
            console.log('blur');
        });
        t.addEventListener('focus',function(){
            console.log('focus');
        });
    </script>
</body>

🖥️ 3. mouse 이벤트

EX)

<head>
	...
   <style>
        body{
            background-color: black;
            color: white;
        }
        #target{
            width: 200px;
            height: 200px;
            background-color: green;
            margin: 10px;
        }
        
        table{
            border-collapse: collapse;
            margin: 10px;
            float: left;
            width: 200px;
        }
        td, th{
            padding: 10px;
            border: 1px solid gray;
        }
    </style>
</head>
<body>
    <!--
        click       : 클릭시 발생
        dblclick    : 더블클릭시 발생
        mousedown   : 마우스를 누를 대 발생
        mouseup     : 마우스 버튼을 땔 때 발생
        mousemove   : 마우스를 움직을 때
        mouseover   : 마우스가 엘리먼트에 진입할때
        mouseout    : 마우스가 엘리먼트에서 빠져나갈 때 발생
        contextmenu : 컨텍스트 메뉴가 실행될 때 발생
    -->
    <div id="target"></div>
    <table>
        <tr>
            <th>event type</th>
            <th>info</th>
        </tr>
        <tr>
            <td>click</td>
            <td id="elmclick"></td>
        </tr>
        <tr>
            <td>dblclick</td>
            <td id="elmdblclick"></td>
        </tr>
        <tr>
            <td>mousedown</td>
            <td id="elmmousedown"></td>
        </tr>
        <tr>
            <td>mouseup</td>
            <td id="elmmouseup"></td>
        </tr>
        <tr>
            <td>mousemove</td>
            <td id="elmmousemove"></td>
        </tr>
        <tr>
            <td>mouseover</td>
            <td id="elmmouseover"></td>
        </tr>
        <tr>
            <td>mouseout</td>
            <td id="elmmouseout"></td>
        </tr>
        <tr>
            <td>contextmenu</td>
            <td id="elmcontextmenu"></td>
        </tr>
    </table>
  

0개의 댓글