<div> ๋ผ๋ ํ๊ทธ๋ ์๋ฌด ์๋ฏธ๊ฐ ์๋ ํ๊ทธ์ด๋ค. ๋์ css๋ js๋ก ์ ์ดํ๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ ํ๊ทธ๋ผ๊ณ ํ๋ฉด ๋จ.
๊ทธ๋ฆฌ๊ณ ํ๋ฉด ์ ์ฒด๋ฅผ ์ฐ๋ block ํ๊ทธ์ด๊ธฐ ๋๋ฌธ์ ์๋์ผ๋ก ์ค๋ฐ๊ฟ์ด ์ผ์ด๋๋ค.
<span> div์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ฌด์๋ฌด์ทจ ํ๊ทธ์ด์ง๋ง, lnlineํ๊ทธ ์ด๊ธฐ๋๋ฌธ์ ์ค๋ฐ๊ฟ์ด ์ผ์ด๋์ง ์๋๋ค.
์กฐ๊ฑด๋ฌธ : ์กฐ๊ฑด์ ๋ฐ๋ผ ๋ค๋ฅธ ๊ธฐ๋ฅ์ด ์คํ๋๋๋ก ํ๋ ๊ฒ if๋ฌธ / if else๋ฌธ ๋ฑ
if๋ true ์กฐ๊ฑด์ผ ๋ ์คํ๋๊ณ else๋ false ์กฐ๊ฑด์ผ ๋ ์คํ๋๋ค.
<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';
}
">
์ฝ๋ํด์:
html๊ณผ javascript๊ฐ ํฉ์ณ์ง ํํ. ์ผ๋จ ๊ฒฐ๊ณผ๋ฌผ์ ๋ฒํผ์ด ํ๋ ์๊ธฐ๊ณ ๊ทธ ๋ฒํผ์ ๋๋ฅผ๋๋ง๋ค ๋คํฌ๋ชจ๋ night ์ day ๋ชจ๋๋ก ๋ฐ๋๋ค. value๊ฐ night ๋๊น ์กฐ๊ฑด์ด ์ถฉ์กฑ๋์ if๋ฌธ ์์ ์๋ ๊ฒ๋ค์ด ์คํ๋๋ค. ์ด๋ night๋ฒํผ์ day๋ก ๋ฐ๋๊ฒ ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋์ ๋ค์ ๋ฒํผ์ ๋๋ฅผ๋๋ (onclick)๋ value(๋ฒํผ)๊ฐ day์ธ ์ํ๋๊น false๊ฐ ๋๋ฉฐ ๋ค์ else๋ฌธ์ด ์์๋์ด day๋ชจ๋๋ก ๋ฐ๋๊ฒ ๋๋ค. ์ด๋ value๋ฒํผ๋ night๋ก ๋ฐ๋๊ณ ๊ทธ๋ผ ๋ค์ if๋ฌธ์ด ์คํ๋์ด ๊ณ์ํด์ ๋ฐ๋ณต๋๊ฒ ๋๋ ์ฝ๋์ด๋ค.
<input type="button" value="night" onclick="
let target = document.querySelector('body');
if(this.value === 'night'){
target.style.backgroundColor='black';
target.style.color='white';
this.value='day';
}else{
target.style.backgroundColor='white';
target.style.color='black';
this.value='night';
}
">
์ฝ๋ํด์:
document.querySelector('#night_day')๋ ์ฝ๋์ ์ํด์๋ ์๊ธฐ์์ ์ ํ๊ทธ๋ผ์ this๋ก ๋ฐ๊ฟ ์ธ ์ ์๋ค. ๊ทธ๋ฆฌ๊ณ this๋ก ๋ฐ๊ฟจ์ผ๋ ๋์ด์ id = 'night_day' ๋ผ๋ ํ๊ทธ๋ ํ์๊ฐ ์์ด์ก๋ค. ๊ทธ๋ฌ๋ฏ๋ก ์ญ์ ํ๋๋ฐ, ์ญ์ ํ๊ณ ์คํํด๋ ์ ํ ๋ฌธ์ ์์ด ์คํ๋๋ ๋ชจ์ต์ ๋ณผ ์ ์๋ค. ๊ฑฐ๊ธฐ์ let target ์ด๋ผ๋ ๋ณ์๋ฅผ ๋ง๋ค์ด์ document.querySelector('body'); ๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ์ด์
target์ ์ง์ด๋ฃ๊ธฐ๋งํ๋ฉด ๋ณ์๊ฐ ์๋ํ๊ฒ ๋๋ ๊ฒ์ด๋ค.
๊ทธ๋ฌ๋ฉด ์ฝ๋๋ฅผ ํจ์ฌ๋ ๊ฐ๊ฒฐํ๊ฒ ์์ฑํ ์ ์๋ค.
document.write('<li>1</li>');
while (true) {
document.write('<li>2</li>');
document.write('<li>3</li>');
}
document.write('<li>4</li>');
์ฝ๋ํด์ :
์ฒซ๋ฒ์งธ ์ค์ด ๋จผ์ ์คํ๋๋ค์, ๊ทธ ๋ค์ while ๋ฌธ์ ๋ณด๋๋ฐ true๋ฉด ์ค๊ดํธ ์์ ์๋๊ฑธ ํ๋์ฉ ์คํ์์ผ์ false๊ฐ ๋์ค๋ฉด ๊ทธ๋ ๋๋๋ฉด์ while๋ฌธ ๋ฐ์ ์๋ ์ฝ๋๋ฅผ ์คํ์ํจ๋ค. ์ ๋ ๊ฒ ์คํ์ํค๋ฉด ๋ฌดํ๋ฐ๋ณต์ด ๋ ๊ฒ์
๋๋ค.
3๋ฒ๋ง ๋ฐ๋ณต๋๊ฒ ํ๊ณ ์ถ์ ๋
document.write('<li>ํ๋</li>');
let i = 0;
while (i < 3) {
document.write('<li>๋</li>');
document.write('<li>์
</li>');
i += 1;
}
document.write('<li>๋ท</li>');
์ฝ๋ํด์:
์ผ๋จ ์ ์ผ ๋จผ์ ๋งจ ์์ ์๋ 'ํ๋'๊ฐ ์คํ๋๋ค. i ๋ ํ์ฌ 0์ด๋ค. ๊ทธ๋์ i๋ while ( i < 3 ) ์ด๋ผ๋ ๊ฐ์ด ์ถฉ์กฑ์ด ๋๋ค. ๊ทธ๋ฌ๋ฉด์ ์ฒซ๋ฒ์งธ '๋,์
'์ด ์ฐํ๋ค. i(0) + 1์ ํ๋๊น i๋ ์ด์ 1์ด๋๋ค. ๊ทธ๋ฌ๋ฉด ๋ค์ while์ด ์คํ๋๋ค. ์ญ์ (1<3) ์ถฉ์กฑ๋๋๊น ๋ค์ '๋,์
'์ด ์ฐํ๋ค. 1+1 ํ๋๊น i ๊ฐ 2๊ฐ๋๊ณ ๋ค์ while๋ฌธ ์คํ. (2<3) ์ถฉ์กฑ๋๋๊น ๋ค์ํ๋ฒ ๋ '๋,์
'์ด ์ฐํ๋ค.
ํ์ง๋ง ์ด ์ดํ์ 2+1 ์ 3์ด ๋์ด๋ฒ๋ฆฌ๋๊น while๋ฌธ์ธ (3<3)์ ์ถฉ์กฑ์ด ๋์ง๋ชปํ๊ณ false๊ฐ ๋์ด๋ฒ๋ ค์ ์ฝ๋๊ฐ ๋ฉ์ถ๊ฒ ๋๊ณ while๋ฌธ ์ค๊ดํธ ๋ฐ์ ์๋ ๋ง์ง๋ง '๋ท' ์ด ์คํ๋๋ ๊ฒ์ด๋ค.
- ํ
๊ตฌ๊ธ๋ง์ ์ด์ฌํ ํ์. ์๊ธฐ์ฃผ๋์ ํ์ต์ด ํ์ํ๋ค.