4.1 ์์ฑ ์กฐ์
DOM ์์์ ์์ฑ๊ณผ ์คํ์ผ์ ์กฐ์ํ๋ ๊ฒ์ ์น ๋ฌธ์ ์์ ์ ์ค์ํ ๋ถ๋ถ์ด์ผ. ์ด๊ฑธ ํตํด ์ฌ์ฉ์ ํ๋์ด๋ ๋ค๋ฅธ ์กฐ๊ฑด์ ๋ฐ๋ผ ์์์ ์ธ๊ด๊ณผ ํ๋์ ๋ณ๊ฒฝํ ์ ์์ด.
1. ์์ฑ ์ป๊ธฐ
์์์ ์์ฑ ๊ฐ์ ์ป์ผ๋ ค๋ฉด getAttribute()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด.
์:
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
</head>
<body>
<a href="https://example.com" id="myLink">Visit Example.com</a>
<script>
const link = document.getElementById('myLink');
const href = link.getAttribute('href');
console.log(href); // ์ถ๋ ฅ: https://example.com
</script>
</body>
</html>
2. ์์ฑ ์ค์
์์ฑ์ ๊ฐ์ ์ค์ ํ๊ฑฐ๋ ๋ณ๊ฒฝํ๋ ค๋ฉด setAttribute()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด.
์:
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
</head>
<body>
<a href="https://example.com" id="myLink">Visit Example.com</a>
<script>
const link = document.getElementById('myLink');
link.setAttribute('href', 'https://newexample.com');
console.log(link.getAttribute('href')); // ์ถ๋ ฅ: https://newexample.com
</script>
</body>
</html>
3. ์์ฑ ์ญ์
์์ฑ์ ์ญ์ ํ๋ ค๋ฉด removeAttribute()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด.
์:
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
</head>
<body>
<a href="https://example.com" id="myLink" target="_blank">Visit Example.com</a>
<script>
const link = document.getElementById('myLink');
link.removeAttribute('target');
console.log(link.getAttribute('target')); // ์ถ๋ ฅ: null
</script>
</body>
</html>
4. ์์ฑ ์กด์ฌ ์ฌ๋ถ ํ์ธ
์์ฑ์ ์กด์ฌ ์ฌ๋ถ๋ฅผ ํ์ธํ๋ ค๋ฉด hasAttribute()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด.
์:
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
</head>
<body>
<a href="https://example.com" id="myLink" target="_blank">Visit Example.com</a>
<script>
const link = document.getElementById('myLink');
console.log(link.hasAttribute('target')); // ์ถ๋ ฅ: true
link.removeAttribute('target');
console.log(link.hasAttribute('target')); // ์ถ๋ ฅ: false
</script>
</body>
</html>
4.2 ํด๋์ค ์กฐ์
1. ํด๋์ค ์ถ๊ฐ
์์์ ํด๋์ค๋ฅผ ์ถ๊ฐํ๋ ค๋ฉด classList.add()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด.
์:
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
<style>
.highlight {
background-color: yellow;
}
</style>
</head>
<body>
<p id="myParagraph">This is a paragraph.</p>
<script>
const paragraph = document.getElementById('myParagraph');
paragraph.classList.add('highlight');
</script>
</body>
</html>
2. ํด๋์ค ์ญ์
์์์์ ํด๋์ค๋ฅผ ์ญ์ ํ๋ ค๋ฉด classList.remove()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด.
์:
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
<style>
.highlight {
background-color: yellow;
}
</style>
</head>
<body>
<p id="myParagraph" class="highlight">This is a paragraph.</p>
<script>
const paragraph = document.getElementById('myParagraph');
paragraph.classList.remove('highlight');
</script>
</body>
</html>
3. ํด๋์ค ํ ๊ธ
ํด๋์ค๋ฅผ ํ ๊ธํ๋ ค๋ฉด classList.toggle()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด. ํด๋์ค๊ฐ ์๋ค๋ฉด ์ถ๊ฐํ๊ณ , ์์ผ๋ฉด ์ญ์ ํด์ค.
์:
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
<style>
.highlight {
background-color: yellow;
}
</style>
</head>
<body>
<button type="button" id="myButton">Click me!</button>
<script>
const btn = document.getElementById('myButton');
btn.addEventListener("click", () => {
btn.classList.toggle('highlight'); // ํด๋์ค๋ฅผ ์ถ๊ฐ. ๋ค์ ๋๋ฅด๋ฉด ํด๋์ค๋ฅผ ์ญ์ .
});
</script>
</body>
</html>
4. ํด๋์ค ์กด์ฌ ์ฌ๋ถ ํ์ธ
ํด๋์ค์ ์กด์ฌ ์ฌ๋ถ๋ฅผ ํ์ธํ๋ ค๋ฉด classList.contains()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด.
์:
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
<style>
.highlight {
background-color: yellow;
}
</style>
</head>
<body>
<p id="myParagraph" class="highlight">This is a paragraph.</p>
<script>
const paragraph = document.getElementById('myParagraph');
console.log(paragraph.classList.contains('highlight')); // ์ถ๋ ฅ: true
paragraph.classList.remove('highlight');
console.log(paragraph.classList.contains('highlight')); // ์ถ๋ ฅ: false
</script>
</body>
</html>
4.3 ์คํ์ผ ์กฐ์
1. ์ธ๋ผ์ธ ์คํ์ผ ๋ณ๊ฒฝ
์์์ ์ธ๋ผ์ธ ์คํ์ผ์ ๋ณ๊ฒฝํ๋ ค๋ฉด style
์์ฑ์ ์จ.
์:
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
</head>
<body>
<p id="myParagraph">This is a paragraph.</p>
<script>
const paragraph = document.getElementById('myParagraph');
paragraph.style.color = 'red';
paragraph.style.fontSize = '20px';
</script>
</body>
</html>
2. ๊ณ์ฐ๋ ์คํ์ผ ์ป๊ธฐ
์์์ ํ์ฌ ๊ณ์ฐ๋ ์คํ์ผ์ ์ป์ผ๋ ค๋ฉด window.getComputedStyle()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด.
์:
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
<style>
#myParagraph {
color: blue;
font-size: 18px;
}
</style>
</head>
<body>
<p id="myParagraph">This is a paragraph.</p>
<script>
const paragraph = document.getElementById('myParagraph');
const styles = window.getComputedStyle(paragraph);
console.log(styles.color); // ์ถ๋ ฅ: rgb(0, 0, 255) ๋๋ ํ๋์
console.log(styles.fontSize); // ์ถ๋ ฅ: 18px
</script>
</body>
</html>
3. ์ธ๋ผ์ธ ์คํ์ผ ์ญ์
์ธ๋ผ์ธ ์คํ์ผ์ ์ญ์ ํ๋ ค๋ฉด ์คํ์ผ ๊ฐ์ ๋น ๋ฌธ์์ด๋ก ์ค์ ํ๋ฉด ๋ผ.
์:
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
</head>
<body>
<p id="myParagraph" style="color: red; font-size: 20px;">This is a paragraph.</p>
<script>
const paragraph = document.getElementById('myParagraph');
paragraph.style.color = '';
paragraph.style.fontSize = '';
</script>
</body>
</html>
GO TO FULL VERSION