简易计算机网页

今天我们来讲一个关于制作一个简单的计算机网页的方法,来完成我们网页中简易的运算。注释用红色和蓝色表示,红色为较重要的地方,值得注意

<html lang="en">

<head>

<meta charset="UTF-8">

 <!--记得写上标题,以方便我们更清楚-->

 <title>计算器</title>

<!--建一个js文件夹-->

<script type="text/javascript">

<!--定义一个函数-->

     function show(my){

<!--读num1字符值-->

       var n1=document.getElementById('num1').value;

<!--将num1转化为数值型,以方便计算使用这一步尤为重要-->

        n1=parseInt(n1);
       var n2=document.getElementById('num2').value;
        n2=parseInt(n2);
       var answer='';

        answer=parseInt('answer');

<!--建立一个条件语句,对函数读取的符号值进行判断,以便计算 -->

        switch(my){
        case '+':
        answer=n1+n2;
        break;
        case '-':
        answer=n1-n2;
        break;
        case '*':
        answer=n1*n2;
        break;
        case '/':
        answer=n1/n2;
        break;

        }

<!--输出计算的数-->

        document.getElementById('deshu').value=answer;
     }

</script>

<!--对计算机外部样式进行设置-->

<style type="text/css">
    table{
    width: 400px;
    height: 200px;
    border:1px solid #e1e1e1;
    margin: 10px auto;
    border-collapse: collapse;
    }
    th,td{
    height: 37px;
    border:1px solid #e1e1e1;
    }
    input[type="text"]{
    width: 176px;
    height: 19px;
    border:1px solid #bfbfbf;
    }
    input[type="button"]{
    width: 30px;
    height: 30px;
    background-color: #3fbe5b;
    margin-top: 5px;
        border:0;
        margin-left: 5px;
        text-align: center;
    }


</style>
</head>
<body>
<table>
   <tr>
    <th colspan="3">简易计算器</th>
   </tr>
<tr>
<td>第一个数字</td>

<td>

                               <!--输入第一个,并建立id进行函数读取-->

<input type="text" value="" id="num1" >
</td>

<td rowspan="4">

                              <!--设置运算符号,并读取-->

<input type="button" value="+" οnclick="show('+')"><br>
<input type="button" value="-" οnclick="show('-')"><br>
<input type="button" value="*" οnclick="show('*')"><br>
<input type="button" value="/" οnclick="show('/')"><br>
</td>
</tr>
<tr>
<td>第二个数字</td>

<td>

                             <!--输入第二个数,并进行设置-->

   <input type="text" value="" id="num2">
</td>
</tr>
<tr>

<td>得数</td>

                        <!--输出得数-->

<td><input type="text" id="deshu" value=""></td>
</tr>
<tr>
<td colspan="2">提示信息</td>
</tr>
</table>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值