본문 바로가기
Front-end

[js]자바 스크립트: 이벤트(Event)

by 쟈근꿈틀이 2022. 5. 2.
728x90
  • onclick: 요소가 클릭될 때 호출됨
  • onmouseover: 마우스 포인터가 요소 위에 올라가 있을 때 호출됨
  • onmouseout: 마우스 포인터가 요소를 벗어났을 때 호출됨
  • onchange: 요소 값을 변경한 후 마우스 포인터가 요소를 벗어났을 때 호출됨
  • onmouseup: 마우스 버튼이 눌린 상태가 아닐 때 호출됨
  • onmousedown: 마우스 버튼이 눌려 있을 때 호출됨

 

onchange 예제
<html>
<head>
<meta charset="UTF-8">
<title>ex19</title>
<script>

function pwchk(){
inPut = document.getElementById("inPut").value;
document.getElementById("outPut").value=inPut;
document.getElementById("inPut").value = "";
}
</script>
</head>
<body>
<hr>
<input type="text" id="inPut" onchange="pwchk()"><br>
<input type="text" id='outPut'><br>
<hr>
</body>
</html>

 

id가 inPut인 텍스트 필드에 값을 입력  (마우스가 요소를 벗어나지 않았을 때)

 

다른 곳을 마우스로 클릭하거나 엔터, tab키 등을 클릭하여 마우스가 요소를 벗어났을 때

 

 

onmousseup, onmousedown 예제
<html>
<head>
<meta charset="UTF-8">
<title>ex22</title>
<script>
function turnOn(obj) {
obj.src = "images/lighton.png";
document.getElementById("msg").innerHTML = "Unclick mouse and turn off!";
}
function turnOff(obj) {
obj.src = "images/lightoff.png";
document.getElementById("msg").innerHTML = "Click mouse and turn on!";
}
</script>
</head>
<body>
<img src="images/lightoff.png" width="50px" height="100px" id="lightOff" onmousedown="turnOn(this)" onmouseup="turnOff(this)"/><br>
<p id="msg">Click mouse and turn on!</p>
</body>
</html>

 

마우스 버튼이 눌리지 않았을 때(onmouseup)

 

마우스 버튼이 눌렸을 때          (onmousedown)

 

 

728x90