JavaScript-4

김민성·2023년 3월 14일
0

JavaScript

목록 보기
4/8
post-thumbnail

const h1 = document.querySelector("div.hello:first-childe h1 ");

function handleTitleClick(){
    h1.style.color = "blue";
}

function handleMouseEnter(){
    h1.innerText = "Mouse is here!";
}

function handleMouseLeave(){
    h1.innerText ="Mouse is gone!";
}

function handleWindowResize(){
    document.body.style.backgroundColor="tomato";
}

function handleWindowCopy(){
    alert("copier");
}

function handleWindowOffiline(){
    alert("SoS no WIFI")
}

h1.addEventListener("click",handleTitleClick);
h1.addEventListener("mouseenter",handleMouseEnter);
h1.addEventListener("mouseleave",handleMouseLeave);

window.addEventListener("resize", handleWindowResize);
window.addEventListener("copy", handleWindowCopy);
window.addEventListener("offline", handleWindowOffiline);
  • 이벤트 리스너는 자주 사용되고, 마우스를 올려놓고나, 내려놓으면 글자가 바뀌고, 클릭 시 색깔들이 바뀐다.

const h1 = document.querySelector("div.hello:first-child h1 ");

function handleTitleClick(){
    const currentColor = h1.style.color;
    let newColor;
    if(currentColor === "blue"){
        newColor ="tomato";
    } else {
        newColor = "blue";
    }
    h1.stlye.color = newColor;
}
h1.addEventListener("click", handleTitleClick);
  • 클릭할때마다 파란색으로 바뀌고, 주황색으로 바뀐다.

  • step 1 element를 찾아라
    step 2 event를 listen 해라
    step 3 그 event에 반응해라

profile
처음부터 다시 기본부터 잡아보자

4개의 댓글

comment-user-thumbnail
2023년 3월 15일

자바스크립트 사진을 그림판에서 여백좀 삭제해서 저장해봐여
왼쪽으로 치우쳐져있어서 안이쁨

1개의 답글
comment-user-thumbnail
2023년 3월 15일
style = "display: flex;  justify-content: center; align-items: center;"
1개의 답글