ยซ   2025/02   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Archives
Recent Posts
02-08 13:57

Today
Total

Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

์—ฐ์˜ ๊ธฐ๋ก ๐Ÿช

JavaScript ๋‚ด์šฉ์ •๋ฆฌ - 3 ๋ณธ๋ฌธ

WEB/HTML+CSS+JS

JavaScript ๋‚ด์šฉ์ •๋ฆฌ - 3

kite707 2021. 1. 3. 02:23

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>

๋ฐฐ์—ด[0] ์„ ํ•˜๋ฉด ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

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>

์ž˜ ์ž‘๋™ํ•˜๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.