Events and event handlers

김서하·2021년 4월 18일
0
자바스크립트는 html과 css를 바꾸는 기능을 하지만 이벤트에 반응하기 위해 만들어졌다.

이벤트란???
 웹사이트에서 발생하는 것들을 말한다!!!
click,resize,submit,input,change,load,before,
closing,printing 같은 것들
 
 addEventListener구문
 target.addEventListener(type, listener);
 
 ex)
  function handleResize(){
  console.log("I have been resized")}
  
 resize
 window.addEventListener("resize", handleResize);
 
 //handleResize를 할 때, 뒤에()를 붙이지 않는다.
 ()를 붙이면 함수가 바로 자동적으로 호출되기 때문!!
 
 다른 ex)
  const title = document.querySelector("#title");
  //html에서 ID타이틀을 찾으라고 명령
  title.innerHTML = "Hi from JS";
  //title의 내용변경
  function handleClick(){//함수생성
   title.style.color = "red";//타이틀 색상변경
  }
  title.addEventListener("click", handleClick);
  //타이틀 클릭했을 때 함수 안의 내용이 실행
profile
개발자 지망생 서하입니당

0개의 댓글