js005.html์ ์์ฑํ๊ณ js004.html์ ๋ด์ฉ์ ๋ณต์ฌํ ํ p ํ๊ทธ๋ฅผ ์ถ๊ฐ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div id="alert" class="alert-box" >
<p>์๋ฆผ์ฐฝ ์ ๋ชฉ</p>
<p>์๋ฆผ์ฐฝ ์ธ๋ถ๋ด์ฉ</p>
<button onclick="alertControl('none')">๋ซ๊ธฐ</button>
</div>
<button onclick="alertControl('block', 'black')">๋ฒํผ 1 </button>
<button onclick="alertControl('block', 'red')">๋ฒํผ 2</button>
</body>
<script>
function alertControl(ํ๋ผ๋ฏธํฐ1, ํ๋ผ๋ฏธํฐ2){
document.getElementById('alert').style.display = ํ๋ผ๋ฏธํฐ1;
document.getElementById('alert').style.backgroundColor = ํ๋ผ๋ฏธํฐ2;
}
</script>
</html>
.text {
color : aquamarine;
}
main.css ํ์ผ์ text class์ css๋ฅผ ์ถ๊ฐํด๋ณด์.
p ํ๊ทธ ์ค ์ ๋ชฉ ๋ถ๋ถ๋ง fontSize ๋ฅผ ํค์ฐ๊ณ ์ถ์๋ฐ ์ด๋ป๊ฒ ํด์ผํ ๊น?
์ง๊ธ๊น์ง ํด์๋ ๊ฒ์ฒ๋ผ ์ํ๋ p ํ๊ทธ์ id๋ฅผ ๋ถ์ฌํ๊ณ
document.getElementById('๋ถ์ฌํID').style.fontSize = "50px";
์ด๋ฐ์์ผ๋ก ํด๋ ๋๊ฒ ์ง๋ง ์ด๋ฒ์ ๋ค๋ฅธ ์
๋ ํฐ๋ฅผ ์ฌ์ฉํด๋ณด์.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div id="alert" class="alert-box" >
<p class="text">์๋ฆผ์ฐฝ ์ ๋ชฉ</p>
<p class="text">์๋ฆผ์ฐฝ ์ธ๋ถ๋ด์ฉ</p>
<button onclick="alertControl('none')">๋ซ๊ธฐ</button>
</div>
<button onclick="alertControl('block', 'black')">๋ฒํผ 1 </button>
<button onclick="alertControl('block', 'red')">๋ฒํผ 2</button>
</body>
<script>
function alertControl(ํ๋ผ๋ฏธํฐ1, ํ๋ผ๋ฏธํฐ2){
document.getElementById('alert').style.display = ํ๋ผ๋ฏธํฐ1;
document.getElementById('alert').style.backgroundColor = ํ๋ผ๋ฏธํฐ2;
document.getElementsByClassName('text')[0].style.fontSize = "50px";
}
</script>
</html>
์ด๋ ๊ฒ p ํ๊ทธ์ class๋ฅผ ๋ถ์ฌํ๊ณ
document.getElementsByClassName('text')[0].style.fontSize = "50px";
'text'๋ผ๋ ์ด๋ฆ์ ํด๋์ค ์ค [ ๋ช ๋ฒ์งธ] ์ fontSize๋ฅผ 50px๋ก ๋ณ๊ฒฝํด์ค ๋ผ๊ณ ํ ์ ์๋ค.
์ด์ธ์๋ getElementsByName , getElementsByTagName ๊ณผ ๊ฐ์ ์
๋ ํฐ๊ฐ ์๋ค.