์ฐ์ ๊ธฐ๋ก ๐ช
JavaScript ๋ด์ฉ์ ๋ฆฌ - 3 ๋ณธ๋ฌธ
https://opentutorials.org/course/3085/18879
์กฐ๊ฑด๋ฌธ ์๊ณ - ์ํ์ฝ๋ฉ
์กฐ๊ฑด๋ฌธ ์๊ณ 2017-12-02 00:14:09
opentutorials.org
์ฌ๊ธฐ์ ๋ถํฐ ๊ณต๋ถ ์์์ด๋ค.
์กฐ๊ฑด๋ฌธ ๊ธฐ์ด
์กฐ๊ฑด๋ฌธ์ ๋ค๋ฅธ ํ๋ก๊ทธ๋๋ฐ์ ํด๋ดค๋ค๋ฉด ๋ด์ฉ์ด ๊ฐ์์ ์ค์ต๋ง ํด๋ณด๊ณ ๋์ด๊ฐ๋ค.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Conditional statements</h1>
<h2>Program</h2>
<script>
document.write("1<br>")
document.write("2<br>")
document.write("3<br>")
document.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>
์กฐ๊ฑด๋ฌธ ํ์ฉ
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WEB1 - JavaScript</title>
</head>
<body>
<h1><a href="index.html">WEB</a></h1>
<input type="button" value="night" onclick="
document.querySelector('body').style.backgroundColor='black'; //๋ฐฐ๊ฒฝ์ ๋ฐ๊ฟ๋
document.querySelector('body').style.color='white'; //๊ธ์ ์์ ๋ฐ๊ฟ ๋
">
<input type="button" value="day" onclick="
document.querySelector('body').style.backgroundColor='white'; //๋ฐฐ๊ฒฝ์ ๋ฐ๊ฟ๋
document.querySelector('body').style.color='black'; //๊ธ์ ์์ ๋ฐ๊ฟ ๋
">
<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';
}
">
<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>
<h2>JavaScript</h2>
<p>
JavaScript (/หdสษหvษหskrษชpt/[6]), often abbreviated as JS, is a high-level, dynamic, weakly typed, prototype-based, multi-paradigm, and interpreted programming language. Alongside HTML and CSS, JavaScript is one of the three core technologies of World Wide Web content production. It is used to make webpages interactive and provide online programs, including video games. The majority of websites employ it, and all modern web browsers support it without the need for plug-ins by means of a built-in JavaScript engine. Each of the many JavaScript engines represent a different implementation of JavaScript, all based on the ECMAScript specification, with some engines not supporting the spec fully, and with many engines supporting additional features beyond ECMA.
</p>
</body>
</html>
์์์ ๋ฐฐ์ด๋๋ก querySelector์ ์ด์ฉํด์ ๊ฐ์ ๊ฐ์ ธ์จ ๋ค ๊ทธ ๊ฐ์ด night์ผ๋ day์ผ๋๋ฅผ ๊ตฌ๋ถํ์ฌ ์ฝ๋๋ฅผ ์์ฑํด์ฃผ๋ฉด ๋๋ค.
์์ ๊ฐ์ด ๋ณํ๋ ๊ฒ๊น์ง ์ ๊ด์ฐฐํ ์ ์๋ค.
๋ฆฌํฉํ ๋ง ์ค๋ณต์ ์ ๊ฑฐ
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WEB1 - JavaScript</title>
</head>
<body>
<h1><a href="index.html">WEB</a></h1>
<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';
}
">
<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>
<h2>JavaScript</h2>
<p>
JavaScript (/หdสษหvษหskrษชpt/[6]), often abbreviated as JS, is a high-level, dynamic, weakly typed, prototype-based, multi-paradigm, and interpreted programming language. Alongside HTML and CSS, JavaScript is one of the three core technologies of World Wide Web content production. It is used to make webpages interactive and provide online programs, including video games. The majority of websites employ it, and all modern web browsers support it without the need for plug-ins by means of a built-in JavaScript engine. Each of the many JavaScript engines represent a different implementation of JavaScript, all based on the ECMAScript specification, with some engines not supporting the spec fully, and with many engines supporting additional features beyond ECMA.
</p>
</body>
</html>
์์ ์ฝ๋๋ ์ด๋ ๊ฒ ์์ํ๋ค.
์ด๋ ๊ฒ ๋ณ์ ์ ์ธ์ ํ๊ณ ํ์ํ ๋ถ๋ถ์ target์ผ๋ก ๋ฐ๊ฟ ์ ์๋ค.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WEB1 - JavaScript</title>
</head>
<body>
<h1><a href="index.html">WEB</a></h1>
<input id ="night_day" type="button" value="night" onclick="
var target=document.querySelector('body'); //๋ณ์๋ฅผ ์ ์ธํ๊ณ ์ฝ๋๋ฅผ ๋์ฒดํ๋ค.
if(document.querySelector('#night_day').value==='night'){
target.style.backgroundColor='black';
target.style.color='white';
document.querySelector('#night_day').value='day';
}
else{
target.style.backgroundColor='white';
target.style.color='black';
document.querySelector('#night_day').value='night';
}
">
<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>
<h2>JavaScript</h2>
<p>
JavaScript (/หdสษหvษหskrษชpt/[6]), often abbreviated as JS, is a high-level, dynamic, weakly typed, prototype-based, multi-paradigm, and interpreted programming language. Alongside HTML and CSS, JavaScript is one of the three core technologies of World Wide Web content production. It is used to make webpages interactive and provide online programs, including video games. The majority of websites employ it, and all modern web browsers support it without the need for plug-ins by means of a built-in JavaScript engine. Each of the many JavaScript engines represent a different implementation of JavaScript, all based on the ECMAScript specification, with some engines not supporting the spec fully, and with many engines supporting additional features beyond ECMA.
</p>
</body>
</html>
๋ ์๋์ฒ๋ผ this๋ฅผ ์ด์ฉํด์ ์ฝ๋๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ๋ง๋ค ์ ์๋ค.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WEB1 - JavaScript</title>
</head>
<body>
<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'; //๋ณ๊ฒฝ๋ ๋ถ๋ถ
}
else{
target.style.backgroundColor='white';
target.style.color='black';
this.value='night'; //๋ณ๊ฒฝ๋ ๋ถ๋ถ
}
">
<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>
<h2>JavaScript</h2>
<p>
JavaScript (/หdสษหvษหskrษชpt/[6]), often abbreviated as JS, is a high-level, dynamic, weakly typed, prototype-based, multi-paradigm, and interpreted programming language. Alongside HTML and CSS, JavaScript is one of the three core technologies of World Wide Web content production. It is used to make webpages interactive and provide online programs, including video games. The majority of websites employ it, and all modern web browsers support it without the need for plug-ins by means of a built-in JavaScript engine. Each of the many JavaScript engines represent a different implementation of JavaScript, all based on the ECMAScript specification, with some engines not supporting the spec fully, and with many engines supporting additional features beyond ECMA.
</p>
</body>
</html>
๋ฐฐ์ด
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Array</h1>
<h2>Syntax</h2>
<script>
var coworkers=["egoing","leezche"];
</script>
<h2>get</h2>
<script>
document.write(coworkers[0]);
</script>
</body>
</html>
push
var fruits=["Banana","Orange","Apple","Mango"];
fruits.push("kiwi");
//fruits =["Banana","Orange","Apple","Mango","Kiwi"]
length
var fruits=["Banana","Orange","Apple","Mango"];
document.write(fruits.length);
//๊ฒฐ๊ณผ -> 4
์๋ ๋งํฌ์์ ๋ ๋ง์ ์ ๋ณด๋ฅผ ์ป์ ์ ์๋ค.
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array
Array - JavaScript | MDN
JavaScript Array ์ ์ญ ๊ฐ์ฒด๋ ๋ฐฐ์ด์ ์์ฑํ ๋ ์ฌ์ฉํ๋ ๋ฆฌ์คํธ ํํ์ ๊ณ ์์ค ๊ฐ์ฒด์ ๋๋ค.๋ฐฐ์ด์ ํ๋กํ ํ์ ์ผ๋ก ํ์๊ณผ ๋ณํ ์์ ์ ์ํํ๋ ๋ฉ์๋๋ฅผ ๊ฐ๋, ๋ฆฌ์คํธ์ ๋น์ทํ ๊ฐ์ฒด์ ๋๋ค. JavaSc
developer.mozilla.org
๋ฐ๋ณต๋ฌธ
๋ค๋ฅธ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ๋ฌธ๋ฒ์ด ๊ฑฐ์ ๋น์ทํ๋ค. ์ด์ ๋ฐฐ์ด๊ณผ ๋ฐ๋ณต๋ฌธ์ ํจ๊ป ์ฌ์ฉํด๋ณด๋๋ก ํ์.
<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>'+coworkers[i]+'</li>');
i = i + 1;
}
</script>
</ul>
์ด๋ฐ์์ผ๋ก ๋ฐฐ์ด์ ์๋ ๊ฐ์ ํ๋์ฉ ๊บผ๋ด์์ ๋ฆฌ์คํธ๋ฅผ ๋ง๋๋๋ฐ ์ฌ์ฉํ ์ ์๋ค.
๋ฐฐ์ด๊ณผ ๋ฐ๋ณต๋ฌธ์ ํ์ฉ
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WEB1 - JavaScript</title>
</head>
<body>
<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'; //๋ณ๊ฒฝ๋ ๋ถ๋ถ
}
else{
target.style.backgroundColor='white';
target.style.color='black';
this.value='night'; //๋ณ๊ฒฝ๋ ๋ถ๋ถ
}
">
<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>
<h2>JavaScript</h2>
<p>
JavaScript (/หdสษหvษหskrษชpt/[6]), often abbreviated as JS, is a high-level, dynamic, weakly typed, prototype-based, multi-paradigm, and interpreted programming language. Alongside HTML and CSS, JavaScript is one of the three core technologies of World Wide Web content production. It is used to make webpages interactive and provide online programs, including video games. The majority of websites employ it, and all modern web browsers support it without the need for plug-ins by means of a built-in JavaScript engine. Each of the many JavaScript engines represent a different implementation of JavaScript, all based on the ECMAScript specification, with some engines not supporting the spec fully, and with many engines supporting additional features beyond ECMA.
</p>
</body>
</html>
์ด ์ฝ๋์์ ์ค์ต์ ์์ํ๋ค. ์ฐ์ ๋ง์ฐ์ค ์ฐํด๋ฆญ -> ๊ฒ์ฌ -> Escํค ๋ฅผ ๋๋ฌ ์ฝ์์ฐฝ์ ์ด๋๋ก ํ์.
๊ทธ๋ฐ๋ค์ <a>ํ๊ทธ๋ก ๋๋ฌ์ธ์ธ ๋ถ๋ถ์ ๊ฐ์ ธ์ค๋ ์๋๋ฅผ ํ๋ค. queruSelector์ ํด๋น ํ๊ทธ๋ก ๊ฐ์ธ์ง ์ฒซ๋ฒ์งธ ํ๊ทธ ๋ด์ฉ๋ง์ ๊ฐ์ ธ์ค๊ธฐ ๋๋ฌธ์ querySelectorAll์ ์ฌ์ฉํ๋ค. ์๋ ์ฌ์ง์ ๋ณด๋ฉด ๊ฒฐ๊ณผ๊ฐ์ด ๋ฆฌ์คํธ์ ํํ๋ก ๋ฆฌํด๋จ์ ์ ์ ์๋ค.
์ฝ์์ฐฝ์์ ์๋์ ๊ฐ์ด ํ๋ฉด ๊ฐ์ ์ฝ์์ฐฝ์์ ๋ณผ ์ ์๋ค.
consle.log("์ถ๋ ฅํ ๋ด์ฉ")
์ด๊ฒ๊ณผ ๋ฐ๋ณต๋ฌธ์ ์ด์ฉํด <a>ํ๊ทธ๋ก ๊ฐ์ธ์ง ๋ถ๋ถ์ ๋ชจ๋ ์ถ๋ ฅํด๋ณด์. ์ฝ๋๋ ์ด๋ ๊ฒ ์ ๋ ฅํ๋ฉด ๋๋ค.
var alist=document.querySelectorAll('a');
var i=0;
while(i<alist.length){
console.log(alist[i]);
i=i+1;
}
์ด๋ฅผ ์ด์ฉํ์ฌ ์ผ๊ด์ ์ผ๋ก ๊ฐ์ ๋ฐ๊ฟ๋ณด๋๋ก ํ์.
var alist=document.querySelectorAll('a');
var i=0;
while(i<alist.length){
console.log(alist[i]);
i=i+1;
}
์ด๋ฅผ ์ด์ฉํด์ ์ ์ฒด ์ฝ๋๋ฅผ ์์ ํ๋ฉด ์๋์ ๊ฐ๋ค.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WEB1 - JavaScript</title>
</head>
<body>
<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;
}
}
">
<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>
<h2>JavaScript</h2>
<p>
JavaScript (/หdสษหvษหskrษชpt/[6]), often abbreviated as JS, is a high-level, dynamic, weakly typed, prototype-based, multi-paradigm, and interpreted programming language. Alongside HTML and CSS, JavaScript is one of the three core technologies of World Wide Web content production. It is used to make webpages interactive and provide online programs, including video games. The majority of websites employ it, and all modern web browsers support it without the need for plug-ins by means of a built-in JavaScript engine. Each of the many JavaScript engines represent a different implementation of JavaScript, all based on the ECMAScript specification, with some engines not supporting the spec fully, and with many engines supporting additional features beyond ECMA.
</p>
</body>
</html>
์ ์๋ํ๋ ๋ชจ์ต์ ๋ณผ ์ ์๋ค.
'WEB > HTML+CSS+JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐ๋๋ผ ์คํฌ๋ฆฝํธ - part 2 ์ ๋ฆฌ (0) | 2021.01.04 |
---|---|
JavaScript ๋ด์ฉ์ ๋ฆฌ - 4 (0) | 2021.01.04 |
JavaScript ๋ด์ฉ์ ๋ฆฌ - 2 (0) | 2021.01.02 |
JavaScript ๋ด์ฉ์ ๋ฆฌ - 1 (0) | 2021.01.01 |
CSS ๋ด์ฉ์ ๋ฆฌ - ์ ํ์ (0) | 2020.12.31 |