Java Script

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>hello</title>

    </head>

    <body>

        <script>

            document.writeln(  /* 사용자에게 보여지는 */

                "<p style='color:red;font-size:30pt'>Hello, JavaScript!!!</p>");  

            console.log("Hello, World"); /* 개발자에게 보여주는 */

        </script>

    </body>

</html>

-------------------------------------------------

입력 받는

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>hello</title>

        <style>

            body{ background-color: yellow}

        </style>

        <script>

            function hello(){  /* body가 로딩이 끝나면 */  

                var irum = prompt("당신의 이름은 ? ", "");

                alert("Welcome!!!" + irum + "님!!!");

            }

        </script>

    </head>

    <body onload="hello()">  

        

    </body>

</html>

----------------------------------

입력, 출력

hello2.html

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>hello</title>

        <link rel="stylesheet" href="css/style.css">

        <script src="js/javascript.js"></script>

    </head>

    <body>

        <h2>이름을 입력해 주세요.</h2>

        <form>

            <p><label>성을 입력해 주세요. 

                <input type="text" name="familyName"></label></p>

            <p><label>이름을 입력해 주세요.

                <input type="text" name="irum"></label></p>    

            <p><input type="button" value="전송하기" onclick="setFullname()"></p>

            <p><input type="text" name="result"></p>

        </form>

    </body>

</html>


javascript.js

function setFullname(){

    var firstName = document.forms[0].familyName.value;

    var secondName = document.forms[0].irum.value;

    

    var fullName = firstName + " " + secondName;

    document.forms[0].result.value = fullName;    

}


style.css

body {

    background-color: white;

    font-family: Arial, Helvetica, Sans-serif;

}

input[type="text"]{

    width:50px;

    border:1px solid #ccc;

}


input[type="button"]{

    font-size:25pt;

    background-color: navy;

    color:red;

}

input[name="result"]{

    width:200px;

    border:none;

    font-size:1.5em;

    color:green;

}

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

두 정수의 곱 계산

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>hello3</title>

        <style>

            body{

                font-size:2em;

            }

            input[type="number"], input[type="text"]{

                font-size:30pt;

                background-color: yellow;

                text-align:right;

                width:150px;

            }

            input[type="button"]{

                font-size:1.5em;

            }

        </style>

        <script>

            function calc(){  /* 자바스크립트에서는 텍스트박스의 내용이 자동정수로 바뀜. 더하기는 안됨 */ 

                var result = document.forms[0].firstNumber.value *

                                  document.forms[0].secondNumber.value;

                document.forms[0].result.value = result;

            }

        </script>

    </head>

    <body>

        <h2>계산기</h2>

        <form>

            <input type="number" name="firstNumber" value="0"> X

            <input type="number" name="secondNumber" value="0"> = 

            <input type="text"  name="result">&nbsp;&nbsp;&nbsp;

            <input type="button" value="계산하기" onclick="calc()">

        </form>

    </body>

</html>

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

버튼 색 클릭으로 백그라운드 색 바꾸는

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>Hello4</title>

        <style>

            #red{  color:red;font-size:25pt;background-color: blue;}

             #blue { color:blue;font-size:25pt;background-color: red;}

        </style>

        <script>

            function change(str){

                if(str == "red"){

                    document.body.style.backgroundColor = '#ff0000';

                }else{

                    document.body.style.backgroundColor = '#0000ff';

                }

            }

        </script>

    </head>

    <body>

        <button id="red" onclick="change('red')">RED</button>&nbsp;&nbsp;&nbsp;

        <button id="blue" onclick="change('blue')">BLUE</button>

    </body>

</html>

만약 onclick를 onmouseover로 바꾸면 마우스가 버튼 위로 올라갔을 때 색 변경됨

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

곱 계산을 js파일 html 파일 나눠서

<!DOCTYPE html>

<html lang="ko">

    <head>

        <meta charset="utf-8">

        <title>JavaScript 위치</title>

        <style>

            body{

                font-size:2em;

            }

            input[type="number"], input[type="text"]{

                font-size:30pt;

                background-color: yellow;

                text-align:right;

                width:150px;

            }

            input[type="button"]{

                font-size:1.5em;

            }

        </style>

    </head>

    <body>

        <h2>계산기</h2>

        <form>

            <input type="number" name="firstNumber" value="0"> X

            <input type="number" name="secondNumber" value="0"> = 

            <input type="text"  name="result">&nbsp;&nbsp;&nbsp;

            <input type="button" value="계산하기" onclick="calc()">

        </form>

        <script src="js/calc.js"></script>

    </body>

</html>


calc.js

function calc(){

    var result = document.forms[0].firstNumber.value *

                      document.forms[0].secondNumber.value;

    document.forms[0].result.value = result;

}

자바 스크립트 파일 선언 <script src="js/calc.js"></script> 은 body의 제일 밑이나 head에 있어야 한다.(body가 모두 실행되고 나서 실행 되어야하므로)

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

구구단 5단

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>Demo</title>

        <style>

            .aaa{

                font-family: "Lucida Fax", Tahoma, Helvetica, Arial, Verdana, Serif;

                display:inline-block;

                margin:30px;

                font-size:25pt;

            }

        </style>

        <script>

            //1. HTML 코드를 생산한다.

            function myfun(n){

                var htmlStr = "<h2>" + n + "단</h2>";

                htmlStr += "<p class='aaa'>";

                for(var i = 1 ; i <= 9; i++){

                    htmlStr += n + " x " + i + " = " + n * i + "<br>";

                }

                

                htmlStr += "</p>";

                document.write(htmlStr);

            }

            myfun(5);

        </script>

    </head>

    <body>

        

    </body>

</html>

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

두개의 수 받아서 사칙연산

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>Demo1</title>

        <style>

            body{

                font-size:2em;

            }

            input[type="number"], input[type="text"]{

                font-size:30pt;

                background-color: yellow;

                text-align:right;

                width:150px;

            }

            input[type="button"]{

                font-size:1.5em;

            }

            select {

                font-size:2em;

            }

        </style>

        <script>

            //연산을 수행한다.

            function calc(){

                var first = document.forms[0].firstNumber.value;

                var second = document.forms[0].secondNumber.value;

                var op = document.forms[0].operator.value;

                var result;

                switch(op){

                    case "더하기":  result = parseInt(first) + parseInt(second); break;   /* 더하기는 parseInt 사용하여 정수로 바꿔줘야함 (다른 연산처럼 자동으로 계산안됨)*/

                    case "빼기":  result = first - second ; break;

                    case "곱하기": result = first * second ; break;

                    case "나누기": result = first / second; break;

                }

                document.forms[0].result.value = result;

            }

        </script>

    </head>

    <body>

        <h2>계산기</h2>

        <form>

            <input type="number" name="firstNumber" value="0">

            <select name="operator">

                <option value="더하기">+</option>

                <option value="빼기">-</option>

                <option value="곱하기">x</option>

                <option value="나누기">/</option>

            </select>

            <input type="number" name="secondNumber" value="0"> = 

            <input type="text"  name="result" disabled>&nbsp;&nbsp;&nbsp;

            <input type="button" value="계산하기" onclick="calc()">

        </form>

    </body>

</html>

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

비만도계산

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>Demo3</title>

        <style>

            *{

                margin:0;padding:0;

            }

            #wrapper{

                width:700px;

            }

            form{

                float:left;

            }

            fieldset{

                width:300px;

                height:250px;

                border : 1px solid #ccc;

                padding:30px;

            }

            legend{

                font-size:1.5em;

                font-weight:900;

            }

            form p{

                font-size:1.2em;

                padding:5px;

            }

            input[type="text"]{

                width:170px;

                text-align:right;

                font-size:1.5em;

            }

            input[type="button"], input[type="reset"]{

                width:150px;

                font-size:1.5em;

                background-color:yellow;

                margin-left:10px;

            } 

            #result {

                font-size:1.5em;

            }

 

        </style>

        <script>

            window.onload = init;

            function init(){

                document.getElementById("mybtn").addEventListener("click", calc, false);   /* click 이벤트 발생. 함수 calc 호출. false는 전파 안하겠다는 의미 */

            }

            function calc(){

                var height = document.forms[0].height.value / 100;

                var weight = document.forms[0].weight.value;

                var bmi = weight / (height * height);

                var result = document.forms[0].irum.value + "님!!!<br>";

                if(bmi < 18.5){

                    result += "<span style='color:orange;'>저체중(";

                }else if(bmi >= 18.5 && bmi < 25.0){

                    result += "<span style='color:green;'>정상(";

                }else if(bmi >= 25.0 && bmi < 30.0){

                    result += "<span style='color:red;'>과체중(";

                }else if(bmi >= 30.0){

                    result += "<span style='color:red;'>비만(";

                }

                result += bmi + ")</span>";

                document.getElementById('result').innerHTML = result;

            }

        </script>

    </head>

    <body>

        <div id="wrapper">

        <h2>BMI</h2>

        <form>

            <fieldset>

                <legend>Private Information</legend>

                <p><label>이름 : <input type="text" name="irum"></label></p>

                <p><label>신장 : <input type="text" name="height"></label></p>

                <p><label>체중 : <input type="text" name="weight"></label></p>

                <p id="result">Result</p>

            </fieldset>

            <input type="button" id="mybtn" name="mybtn" value="계산하기">&nbsp;&nbsp;&nbsp;

            <input type="reset" value="초기화하기">

        </form>

        </div>

    </body>

</html>

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

max , min

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>JavaScript Demo</title>

        <script>

            window.addEventListener("load", aaa, false);

            function aaa(){

                var max = Number.MAX_VALUE;

                var min = Number.MIN_VALUE;

                var str = "JavaScript's 가장 큰 값 = " + max + "<br>";

                str += "가장 작은 값 = " + min + " 입니다.";

                document.getElementById('result').innerHTML = str;

            }    

        </script>

    </head>

    <body>

        <p id="result">여기</p>

    </body>

</html>

출력:

JavaScript's 가장 큰 값 = 1.7976931348623157e+308

가장 작은 값 = 5e-324 입니다.

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

+ 할 때 조심

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>JavaScript Demo</title>

        <script>

            window.addEventListener("load", checkNumber, false);

            

            function checkNumber(){

                var testA = "1000" + "1";

                var testB = "1000" - "1";

                var testC = "1000" * "5";

                var testD = "1000" / "25";

                

                var str = "\"1000\" + \"1\" = " + testA + "<br>";

                str += "\"1000\" - \"1\" = " + testB + "<br>";

                str += "\"1000\" x \"5\" = " + testC + "<br>";

                str += "\"1000\" / \"25\" = " + testD;

                

                document.getElementById('result').innerHTML = str;

            }    

        </script>

    </head>

    <body>

        <p id="result">여기</p>

    </body>

</html>

출력:

"1000" + "1" = 10001

"1000" - "1" = 999

"1000" x "5" = 5000

"1000" / "25" = 40

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

변수의 lifecycle

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>JavaScript Demo</title>

        <script>

            //변수 scope(lifecycle)

            testA();

            testB();

            

            function testA(){

                var scope = "local_A";

                document.write(scope);  //local_A

            }

            function testB(){

                document.write(scope);

            }

        </script>

    </head>

    <body>

        

    </body>

</html>

출력은 local_A 가 하나만나오게된다.

그리고 크롬에서 열었을 떄 ctrl+shift+j 를 눌러서 보면 scope is not defined 의 에러가 뜬다.

--------------------

변수의 lifecycle2

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>JavaScript Demo</title>

        <script>

            //variable's scope

            testA();

            

            function testA(){

                var scope = "local_A";

                document.write(scope);

                testB();

                

                function testB(){

                    document.write(scope);

                }    

            }

        </script>

    </head>

    <body>

        

    </body>

</html>

출력:

local_Alocal_A

----------------------

변수의 lifecycle3

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>JavaScript Demo</title>

        <script>

            //variable's scope

            testA();

            

            function testA(){

                var scope = "local_A";

                document.write(scope);

                testB();

                

                function testB(){

                    var scope = "local_B";

                    document.write(scope);

                }    

            }

        </script>

    </head>

    <body>

        

    </body>

</html>

출력:

local_Alocal_B

------------------------

변수의 lifecycle4

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>JavaScript Demo</title>

        <script>

            //variable's scope

            testA();

            

            function testA(){

                var scope = "local_A";

                document.write(scope);

                testB();

                

                function testB(){

                    scope = "local_B";

                    document.write(scope);

                }    

                document.write(scope);

            }

        </script>

    </head>

    <body>

        

    </body>

</html>

출력:

local_Alocal_Blocal_B 

testB() 에서 scope를 var scope 로 정의해주면 마지막 출력은 local_B가 아닌 local_A가 나온다.

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

변수의 lifecycle5

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>JavaScript Demo</title>

        <script>

            //variable's scope

            var scope = "global";

            testA();

            testB();

            

            function testA(){

                document.write(scope);

            }

            function testB(){

                document.write(scope);

            }

        </script>

    </head>

    <body>

        

    </body>

</html>

출력:

globalglobal

scope 을 global variable 로해서 어디서든 출력 가능.

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

값복사

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>JavaScript Demo</title>

        <script>

            //variable's scope

            var a = true;   //boolean's value

            var b = a;     //값복사

            a = false;

            document.write(b);

        </script>

    </head>

    <body>

        

    </body>

</html>

출력:

true

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

배열의 값 복사 : 배열은 객체이므로 주소복사이기 때문에 값이 변하면 따라 바뀜

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>JavaScript Demo</title>

        <script>

            var a = [1,2,3,4];

            var b = a;

            a[0] = 100;

            document.write(b[0]);

        </script>

    </head>

    <body>

        

    </body>

</html>

출력:

100

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

함수 복사 : 함수도 객체이므로 주소복사

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>JavaScript Demo</title>

        <script>

            var a = function(name){

                document.write(name + "님! 환영합니다.");

            };

            var b = a;   //주소복사

            b("홍길동");

            

        </script>

    </head>

    <body>

        

    </body>

</html>

출력:

홍길동님! 환영합니다.

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

변수의 lifecycle

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>JavaScript Demo</title>

        <script>

            //variable's scope

            testA();

            

            function testA(){

                var scope = 'local_A';

                document.write(scope + "<br>");

                

                testB();

                function testB(){

                    document.write(scope + "<br>");

                    var scope = 'local_B';

                    document.write(scope + "<br>");

                }

                document.write(scope + "<br>");

            }

        </script>

    </head>

    <body>

        

    </body>

</html>

출력:

local_A

undefined

local_B

local_A

위에서 undefined는 만들어져있는데 값이 없는 것이다. 이것은 이름을 scope라고 같이써서 그런 것이므로 이런문제가 발생했다.

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

변수의 lifecycle

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <title>Variable</title>

        <style>

            

        </style>

        <script>

            function first_function(){

                a = 1;

                document.write("first_function's a: " + a +"<br></br>");

                second_function();

                document.write("first_function's a : " +a+ "<br><br>");

            }

            function second_function(){

                var a= 100;

                document.write("second_fuction's a : " +a+ "<br><br>");

            }

        </script>

    </head>

    <body>

        <script>

            first_function();

        </script>

    </body>

</html>

출력:

first_function's a: 1


second_fuction's a : 100


first_function's a : 1

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

계산 퀴즈
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>계산 퀴즈</title>
<link rel="stylesheet" href="css/style1.css">
<script src="js/script.js"></script>
</head>
<body>
<div id="wrapper">
<form>
<fieldset>
<legend>계산 퀴즈</legend>
<p> 이름과 계산의 답을 입력 한 후 완료 단추를 눌러주세요.</p>
<p>이름: <input type="text" name="examinee"></p>
<p><span class="qnumber">1.</span> 5 + 6 = <input type="number" id="question01"></p>
<p><span class="qnumber">2.</span> 8 - 3 = <input type="number" id="question02"></p>
<p><span class="qnumber">3.</span> 7 × 6 = <input type="number" id="question03"></p>
<p><span class="qnumber">4.</span> 15 ÷ 3 = <input type="number" id="question04"></p>
<p><span class="qnumber">5.</span> 8 + 6 - 3 × 2 ÷ 2 = <input type="number" id="question05"></p>
<p><input type="button" name="button" value="완료"></p>
</fieldset>
</form>
<div id="resultarea"></div>
</div>
</body>
</html>

style.css
body {
font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif;
}

#wrapper {
width: 690px;
}

form {
float: left;
}

fieldset {
width: 280px;
height: 300px;
border: 1px solid #aba23c;
padding: 30px;
}

legend {
font-size: 1.5em;
font-weight: bold;
}

form p {
font-size: 0.9em;
}

input[type="number"] {
width: 50px;
float: right;
}

input[type="text"] {
width: 70px;
}

fieldset p:first-of-type {
color: #888;
font-size: 0.8em;
margin-top: 0;
}

fieldset p:nth-of-type(2) {
text-align: right;
}

.qnumber {
margin-right: 15px;
color: #999;
font-weight: bold;
}

input[type="button"] {
width: 70px;
}

fieldset p:last-of-type {
position: relative;
width: 70px;
margin: 30px auto 0;
}

#resultarea {
width: 280px;
height: 286px;
margin-top: 14px;
padding: 30px;
float: right;
border: 1px solid #aba23c;
visibility: hidden;
}

#resultarea p {
color: #888;
font-size: 0.8em;
}

#score {
color: red;
font-size: 1.2em;
font-weight: bold;
}

scripts.js
/**
 * @author Instructor
 */
function setFullname(){
    var firstName = document.forms[0].familyName.value;
    var secondName = document.forms[0].irum.value;
    
    var fullName = firstName + " " + secondName;
    document.forms[0].result.value = fullName;    
}
----------------
계산퀴즈 -내꺼
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Demo3</title>
        <style>
            *{
                margin:0;padding:0;
            }
            #wrapper{
                width:700px;
            }
            form{
                float:left;
            }
            fieldset{
                width:400px;
                height:400px;
                border : 1px solid #ccc;
                padding:30px;
            }
            legend{
                font-size:1.5em;
                font-weight:900;
            }
            form p{
                font-size:1.2em;
                padding:5px;
            }
             input[type="text"]{
                float:right;
                width:50px;
                font-size:1em;
            }
            input[type="button"], input[type="reset"]{
                width:150px;
                font-size:1.5em;
                background-color:yellow;
                margin-left:10px;
                align:center;
            } 
            label{
                font-size:1.5em;
            }
            #result {
                position:absolute;
                left:470px;
                top:-1px;
            }
        </style>
        <script>
            window.onload = init;
            function init(){
                document.getElementById("complete").addEventListener("click", calc, false);   /* click 이벤트 발생. 함수 calc 호출. false는 전파 안하겠다는 의미 */
            }
            function calc(){
                var sum = 0;
                var ans = [11, 5, 42, 5, 11];
                var ans1 = document.forms[0].num1.value;
                var ans2 = document.forms[0].num2.value;
                var ans3 = document.forms[0].num3.value;
                var ans4 = document.forms[0].num4.value;
                var ans5 = document.forms[0].num5.value;
                if(ans[0]==ans1){
                    sum += 20;
                    document.forms[0].num1.style.backgroundColor= '#D5D5D5';

                } else {
                    document.forms[0].num1.style.backgroundColor= '#ff0000';
                }
                if(ans[1]==ans2){
                    sum += 20;
                    document.forms[0].num2.style.backgroundColor= '#D5D5D5';
                } else {
                    document.forms[0].num2.style.backgroundColor= '#ff0000';
                }
                if(ans[2]==ans3){
                    sum += 20;
                    document.forms[0].num3.style.backgroundColor= '#D5D5D5';
                } else {
                    document.forms[0].num3.style.backgroundColor= '#ff0000';
                }
                if(ans[3]==ans4){
                    sum += 20;
                    document.forms[0].num4.style.backgroundColor= '#D5D5D5';
                } else {
                    document.forms[0].num4.style.backgroundColor= '#ff0000';
                }
                if(ans[4]==ans5){
                    sum += 20;
                    document.forms[0].num5.style.backgroundColor= '#D5D5D5';
                } else {
                    document.forms[0].num5.style.backgroundColor= '#ff0000';
                }
                var result = "<fieldset>" +document.forms[0].name.value + "님의 2014년 5월 27일 계산 퀴즈 결과는 " + "<span style='color:red;'>" +sum + "</span>입니다.</fieldset>";
                
                document.getElementById('result').innerHTML = result;
            }
        </script>
    </head>
    <body>
        <div id="wrapper">
        <form>
            <fieldset>
                <legend>계산 퀴즈</legend>
                <p>이름과 계산의 답을 입력 한 후 완료 단추를 눌러주세요.</p>
                <p><label>이름 : <input type="text" name="name"></label></p>
                <p><label>1. 5 + 6 = <input type="text" name="num1"></label></p>
                <p><label>2. 8 - 3 = <input type="text" name="num2"></label></p>
                <p><label>3. 7 x 6 = <input type="text" name="num3"></label></p>
                <p><label>4. 15 ÷ 3 = <input type="text" name="num4"></label></p>
                <p><label>5. 8 + 6 - 3 x 2 ÷ 2 = <input type="text" name="num5"></label></p>
            </fieldset>
            <p id="result"></p>
            <input type="button" id="complete" name="complete" value="완료">&nbsp;&nbsp;&nbsp;
            
        </form>
        </div>
    </body>
</html>


+ Recent posts