JS가 HTML에서 더하기 빼기 곱하기

25363 단어 html


<
script type="text/javascript"> function fun(t){ var fa = parseInt(document.getElementById('fa').value); var fb = parseInt(document.getElementById('fb').value); var fc = document.getElementById('fc'); if(isNaN(fa) || isNaN(fb)){ alert(""); return; } switch(t){ case 1: fc.value = fa + fb; break; case 2: fc.value = fa - fb; break; case 3: fc.value = fa * fb; break; case 4: fc.value = fa / fb; break; } } </script> </head> <body> <input type="text" id="fa" value=""/> <input type="text" id="fb" value=""/> <input type="text" id="fc" value=""/> <input type="button" value="+" onclick="fun(1);" /> <input type="button" value="-" onclick="fun(2);" /> <input type="button" value="x" onclick="fun(3);" /> <input type="button" value="/" onclick="fun(4);" />



 
HTML 계산기
 
<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8" />

        <title> </title>

    </head>

    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>

    <script type="text/javascript">

        $(function(){

            $("button").click(function(){

                var first  = parseInt($("#first").val());// 

                var second = parseInt($("#second").val());// 

                var sum    = 0;// 

                var symbol = parseInt($("[type=radio]:checked").val());// 

                // 

                switch(symbol){

                    case 0:

                        sum = first + second;

                        break;

                    case 1:

                        sum = first - second;

                        break;

                    case 2:

                        sum = first * second;

                        break;

                    case 3:

                        sum = first / second;

                        break;

                }

                var arr    = ['+','-','*','/'];// 

                $("h2 span").text(first + arr[symbol] + second +" = " +sum);//  

            })         

        })

    </script>

    <body>

        <h1> </h1>

        <p><input type="text" id="first" style="width: 40px;" /></p>

        <p>

            <label><input type="radio" name="symbol" value="0" checked="checked"/>+</label>

            <label><input type="radio" name="symbol" value="1" />-</label>

            <label><input type="radio" name="symbol" value="2" />*</label>

            <label><input type="radio" name="symbol" value="3" />/</label>

        </p>

        <p><input type="text" id="second" style="width: 40px;" /></p>

        <p><button> </button></p>

        <h2><span></span></h2>

    </body>

</html>

좋은 웹페이지 즐겨찾기