์กฐ๊ฑด๋ฌธ์ ํ์ฉ
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Conditional statements</h1>
<h2>Program</h2>
<script>
documnt.write("1<br>");
documnt.write("2<br>");
documnt.write("3<br>");
documnt.write("4<br>");
</script>
<h2>IF-true</h2>
<script>
document.write("1<br>");
if(true){
document.write("2<br>");
}
else{
document.write("3<br>");
}
document.write("4<br>");
</script>
<h2>IF-false</h2>
<script>
document.write("1<br>");
if(false){
document.write("2<br>");
}
else{
document.write("3<br>");
}
document.write("4<br>");
</script>
</body>
</html>
์ด์ ์ค์ ๋ก ์ด๋ป๊ฒ ์ฌ์ฉ์ด ๋๋์ง ์ดํด๋ณด๋๋ก ํ๊ฒ ๋ค.
<input id="night_day" type="button" value="night" onclick="
if(document.querySelector('#night_day').value === 'night'){
document.querySelector('body').style.backgroundColor = 'black';
document.querySelector('body').style.color = 'white';
document.querySelector('#night_day').value = 'day';
} else {
document.querySelector('body').style.backgroundColor = 'white';
document.querySelector('body').style.color = 'black';
document.querySelector('#night_day').value = 'night';
}
">
์ด ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ฉด ํ๋์ ๋ฒํผ์ผ๋ก ๋คํฌ๋ชจ๋๋ฅผ ๋ง๋ค ์ ์๋ค.
์ด ์ํ์์ night ๋ฒํผ์ ๋๋ฅด๋ฉด
์ด๋ ๊ฒ ๋คํฌ๋ชจ๋๋ก ๋ฐ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋ค์ day ๋ฒํผ์ ๋๋ฅด๋ฉด
์ด๋ ๊ฒ ์กฐ๊ฑด๋ฌธ์ ํ์ฉํ๋ฉด night, day ๋ฒํผ 2๊ฐ๋ก ์กฐ์ ํ ์ ์์๋ ๋คํฌ๋ชจ๋๋ฅผ ํ ๊ฐ์ ๋ฒํผ์ผ๋ก ์กฐ์ ํ ์ ์๋ค.
'๐ค Language > ๐จ JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JS. ๋ฐฐ์ด๊ณผ ๋ฐ๋ณต๋ฌธ (0) | 2022.01.08 |
---|---|
JS. ๋ฆฌํํ ๋ง ์ค๋ณต์ ๊ฑฐ (0) | 2022.01.07 |
JS. ๋น๊ต ์ฐ์ฐ์์ Boolean ๋ฐ์ดํฐ ํ์ (0) | 2022.01.07 |
JS. ์ ์ดํ ํ๊ทธ ์ ํํ๊ธฐ & querySelector๋ฅผ ์ด์ฉํ ๋คํฌ๋ชจ๋ (0) | 2022.01.06 |
JS. ์น๋ธ๋ผ์ฐ์ ์ ์ด (0) | 2022.01.04 |