Javascript2

김그냥·2023년 1월 27일

console 창에서 html요소들을 보고 변경할 수도 있다.

= 우리는 js에서 html object을 불러올수도 있다.


  1. javascript에서 html을 연결하기

const title = document.getElementById("title");
title.innerText = "got you";
console.log(title.id);
console.log(title.className);


html의 hi를 js에서 got you로 변경하였다.

  • querySelector : 선택기를 이용해 검색한다
  • getElementById : 일치하는 ID를 찾는다

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

function TitleClick() {
    console.log("title was clicked");
}ㅂ

title.addEventListener("click", TitleClick);
const title = document.querySelector("div.hello:first-child h1");

function TitleClick() {
    console.log("title was clicked");
}

title.onclick = TitleClick;

=> hi 를 클릭하면 'title was clicked'가 출력되는 코드이다. (두 코드는 결과가 같음)

event 종류를 찾는 방법


0개의 댓글