React 웹 사이트 클론코딩(작성중)

FE 개발자 신상오·2022년 6월 8일
0

React

목록 보기
2/10
post-thumbnail

바닐라 JS 클릭 버튼 구현 코드

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=
    , initial-scale=1.0">
    <title>Document</title>
</head>
<body>
  <span>Total clicks: 0 </span>
  <button id="btn">Click</button>    
</body>
  <script>
    let counter = 0;
    const button = document.getElementById("btn") // id : btn 
    const span = document.querySelector("span") // span 태그
    function handleClick(){ 
      console.log("I have been click")
      counter += 1
      span.innerText = `Total clicks : ${counter}`; // js 변수 span의 innerText
    }

    button.addEventListener("click", handleClick)
  </script>
</html>

구현 화면

profile
주간 회고용 블로그입니다 (개발일지와 정보글은 티스토리에 작성합니다.)

0개의 댓글