자바스크립트 HTML에 적용하기
<!DOCTYPE html>
<html>
<head>
<title>Something</title>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<h1 id="title">This works!</h1>
<script src="index.js"></script>
</body>
</html>
예제
const title = document.getElementById("title");
title.innerHTML = "Hi, From JS";
title.style.color = "red";
console.dir(title);
결과
자바스크립트로 타이틀 변경하기
예제
const title = document.getElementById("title")
title.innerHTML = "Hi! From JS";
title.style.color = "red";
document.title = "I own you now";
결과
이전에는 타이틀
Something
자바스크립트 적용 후
I own you now
document.QuerySelctor
///id
const title = document.querySelector("#title")
///class
const title = document.querySelector(".title")
노드의 첫번째 자식을 반환한다.
CSS선택자와 비슷해서 id를 찾고 싶으면 "#title"로 작성해야 하고
class를 찾고 싶으면 ".title"로 작성해야 한다.
'노마드코더 > 자바스크립트' 카테고리의 다른 글
#2-5 첫 번째 조건문!! If, else, and, or (0) | 2021.08.29 |
---|---|
#2-4 Events and event handlers (0) | 2021.08.28 |
#2-2 JS DOM Functions (0) | 2021.08.27 |
#2.1.1 More Function Fun (0) | 2021.08.27 |
#2-1 너의 첫번째 함수! (0) | 2021.08.27 |