0. console.dir()
HTML
<div id="first" class="Momentum">
<h1>Grab me!</h1>
</div>
JS
const title = document.querySelector(".Momentum h1");
console.dir(title);
1. getElementById
HTML
<div id="first" class="Momentum">
<h1>Grab me!!</h1>
</div>
JS
const first = document.getElementById("first");
console.log(first);
first.innerHTML = "<h1>I got you!</h1>";
결과:
2. getElementByClassName
HTML
<body>
<h1 class="Momentum">Grab me!</h1>
<h1 class="Momentum">Grab me!</h1>
<h1 class="Momentum">Grab me!</h1>
<h1 class="Momentum">Grab me!</h1>
<h1 class="Momentum">Grab me!</h1>
<script src="app.js"></script>
</body>
JS
const hellos = document.getElementsByClassName("Momentum");
console.log(hellos);
콘솔:
3. getElementByTagName
HTML
<div class="Momentum">
<h1>Grab me!</h1>
</div>
JS
const title = document.getElementsByTagName("h1");
console.log(title);
콘솔:
4. querySelector
HTML
<div class="Momentum">
<h1>Grab me!</h1>
</div>
JS
console.log(document.querySelector(".Momentum h1"));
콘솔 :
5.querySelectorAll
HTML
<body>
<div class="Momentum">
<h1>Grab me!!</h1>
</div>
<div class="Momentum">
<h1>Grab me!!!</h1>
</div>
<div class="Momentum">
<h1>Grab me!!!!</h1>
</div>
<script src="app.js"></script>
</body>
JS
console.log(document.querySelectorAll(".Momentum h1"));
콘솔:
6.getElementByName
7. style
HTML
<div id="first" class="Momentum">
<h1>Grab me!</h1>
</div>
JS
const title = document.querySelector(".Momentum h1");
변경 전:
JS
const title = document.querySelector(".Momentum h1");
console.dir(title);
title.style.color = 'blue';
변경 후: