ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • WEB2 - JavaScript - 조건문, 조건문의 활용
    JavaScript/생활코딩 2018. 12. 24. 14:09






    조건문 - https://opentutorials.org/course/3085/18800



    if문 뒤에는 Boolean타입인 true, false가 온다.

    그리고 true, false 둘 중 어떤것이 선택되느냐에 따라 다른 코드가 실행 된다.



    <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문이 실행되었기 때문에 else문이 무시된다.

      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문이 무시되었기 때문에 else문이 실행된다.

      if(false){

        document.write("2<br>"); 

      } else {

        document.write("3<br>");

      }

      document.write("4<br>");

    </script>









    조건문의 활용 - https://opentutorials.org/course/3085/18878



    if문을 사용하여 버튼을 제어하는 예제이다.


    <h1><a href="index.html">WEB</a></h1>

      <input id="night_day" type="button" value="night" onclick="

        if(document.querySelector('#night_day').value === 'night'){         //버튼의 value값이 night이면 아래 코드를 실행한다.

          document.querySelector('body').style.backgroundColor = 'black';

          document.querySelector('body').style.color = 'white';

          document.querySelector('#night_day').value = 'day';        //value값을 night -> day로 바꾼다.

        } else {

          document.querySelector('body').style.backgroundColor = 'white';    //value값이 day일 때 else문을 실행한다.

          document.querySelector('body').style.color = 'black';

          document.querySelector('#night_day').value = 'night';      //value값을 day -> 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>



    댓글

Designed by Tistory.