개발/vue
JS-HTML
uminisname
2023. 1. 25. 20:37
#01
index.html
Grab me!
왓더?? 여기에 그냥 친다고 다나온다 이게 무슨일임??? 여기가 어디냐고?? index.html 밖인데 이게 무슨ㅇ리이냐?app.js
const title = document.getElementById("title"); //getElementById함수 통해 id로 element 가져올 수 있음
title. innerText = "Got you!"
console.log(title.id);
console.log(title.className);
😀getElementById("") 이안에 id를 html에 있는 id와 맞춰줘야 innerText가 null이라는 오류가 안뜸
//querySelector : .hello h1 .div h1 과 같이 element를 검색할 수 있게 해줌
const title = document.querySelector(".hello h1");
//const title = document.getElementById(".hello h1");
title.innerText = "Hello!"; //이렇게 하면 Grab 1!이게 또 없어짐
console.log(title);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css"/> <!--css와 연결-->
<title>First App</title>
</head>
<body>
<div class="hello">
<h1>Grab me1!</h1>
</div>
<div class="hello">
<h1>Grab me2!</h1>
</div>
<div class="hello">
<h1>Grab me3!</h1>
</div>
<div class="hello">
<h1>Grab me4!</h1>
</div>
<script src="app.js"></script>
</body>
</html>
왓더?? 여기에 그냥 친다고 다나온다 이게 무슨일임???
여기가 어디냐고?? index.html <html></html> 밖인데 이게 무슨ㅇ리이냐?
#02 event
const title = document.querySelector("div.hello:first-child h1");
console.dir(title);
console.log(title);
function handleTitleClick(){
console.log("title was clicked!"); //title클릭시 콘솔에 이 문장 뜸
title.style.color="blue"; //title클릭시 폰트색상 blue로 바뀜
}
title.addEventListener("click", handleTitleClick); //click이벤트
function handleMouseEnter(){
console.log("mouse is here!");
title.innerText = "mouse is here!"; //마우스 접근하면 title이 이텍스트로 바뀜
}
function handleMouseLeave(){
console.log("mouse is gone!");
title.innerText = "mouse is gone!"; //마우스 떠날떄는 title이 이텍스트로 바뀜
}
title.addEventListener("click", handleTitleClick); //click이벤트
title.addEventListener("mouseenter", handleMouseEnter);
title.addEventListener("mouseleave", handleMouseLeave);