๋ฐฐ์ด๊ณผ ๋ฐ๋ณต๋ฌธ
<h1>Array</h1>
<h2>Syntax</h2>
<script>
var coworkers = ["egoing", "leezche"];
</script>
<h2>get</h2>
<script>
document.write(coworkers[0]);
document.write(coworkers[1]);
</script>
<h2>add</h2>
<script>
coworkers.push('duru');
coworkers.push('taeho');
</script>
<h2>count</h2>
<script>
document.write(coworkers.length);
</script>
์๋๋ ๋ฐ๋ณต๋ฌธ์ ๋ฐฐ์ฐ๋ ๊ณผ์ ์ด์๋ค.
์๋ ์ฝ๋๋ฅผ ์คํํ๋ฉด 2,3์ด ์ฐ์์ผ๋ก 3๋ฒ ๋์ฌ ๊ฒ์ด๋ค.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Loop</h1>
<ul>
<script>
document.write('<li>1</li>');
var i = 0;
while(i < 3){
document.write('<li>2</li>');
document.write('<li>3</li>');
i = i + 1;
}
document.write('<li>4</li>');
</script>
</ul>
</body>
</html>
์๋ ์ฝ๋๋ ๋ฐฐ์ด๊ณผ ๋ฐ๋ณต๋ฌธ์ ์ด์ฉํ ๋ก์ง์ด๋ค.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Loop & Array</h1>
<script>
var coworkers = ['egoing', 'leezche', 'duru', 'taeho'];
</script>
<h2>Co workers</h2>
<ul>
<script>
var i = 0;
while(i < coworkers.length){
document.write('<li><a href="http://a.com/'+coworkers[i]+'">'+coworkers[i]+'</a></li>');
i = i + 1;
}
</script>
</ul>
</body>
</html>
coworkers.length ์๋ฆฌ์ ์์๊ฐ์ ๋ฃ์ผ๋ฉด coworkers์ ๋ค์ด๊ฐ ๊ฐ์ด ์ฆ๊ฐํ ๋ ๋ง๋ค ์์๊ฐ์ ๋ณ๊ฒฝํด์ผ ํ๋๋ฐ ๊ฐ๋ฐ์๋
๋น์ง๋์ค ๋ก์ง๊น์ง ๋ณ๊ฒฝํ๋ ๊ฒ์ ๋ง์ด ์๋๋ค.
๊ทธ๋ฌ๋ฏ๋ก coworkers.length๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์์ ๊ฐ ๊ฐฏ์๋ง ๋ฐ๊พธ๋ฉด ๋ ์ ์๋๋ก ํ๋ ๊ฒ์ด๋ค.
๋ค์์ ๋ฐฐ์ด๊ณผ ๋ฐ๋ณต๋ฌธ ํ์ฉ์ ์ด๋ค ์ฝ๋๋ก ํ๋์ง ๋ณผ ๊ฒ์ด๋ค.
์ผ๊ฐ๋ชจ๋์ผ ๋๋ ๊ธ์จ๋ฅผ ๋ฐ๊ฒ ํ์ํ๊ณ ์ฃผ๊ฐ๋ชจ๋์ผ ๋๋ ๊ธ์จ๋ฅผ ๋ณด๋ค ์ด๋ก๊ฒ ์ฒ๋ฆฌํ ๊ฒ์ด๋ค.
<h1><a href="index.html">WEB</a></h1>
<input id="night_day" type="button" value="night" onclick="
var target = document.querySelector('body');
if(this.value === 'night'){
target.style.backgroundColor = 'black';
target.style.color = 'white';
this.value = 'day';
var alist = document.querySelectorAll('a');
var i = 0;
while(i < alist.length){
alist[i].style.color = 'powderblue';
i = i + 1;
}
} else {
target.style.backgroundColor = 'white';
target.style.color = 'black';
this.value = 'night';
var alist = document.querySelectorAll('a');
var i = 0;
while(i < alist.length){
alist[i].style.color = 'blue';
i = i + 1;
}
}
">
<div id="grid">
<ol>
<li><a href="1.html">HTML</a></li>
<li><a href="2.html">CSS</a></li>
<li><a href="3.html">JavaScript</a></li>
</ol>
๋ฐฐ์ด๊ณผ ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ์ฌ ๋งํฌ์ ์์ ๋ฐ๊พธ์๋ค.
์ฃผ๊ฐ ๋ชจ๋์ผ ๋๋ ํ๋์, ์ผ๊ฐ๋ชจ๋์ผ ๋๋ ํ์ฐ๋๋ธ๋ฃจ๋ก ์์ด ๋ค๋ฅธ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์ฃผ๊ฐ ๋ชจ๋
์ผ๊ฐ ๋ชจ๋
'๐ค Language > ๐จ JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JS. ๊ฐ์ฒด (0) | 2022.01.09 |
---|---|
JS. ํจ์ (0) | 2022.01.08 |
JS. ๋ฆฌํํ ๋ง ์ค๋ณต์ ๊ฑฐ (0) | 2022.01.07 |
JS. ์กฐ๊ฑด๋ฌธ (0) | 2022.01.07 |
JS. ๋น๊ต ์ฐ์ฐ์์ Boolean ๋ฐ์ดํฐ ํ์ (0) | 2022.01.07 |