JavaScript实现简单计算器(加减乘除)


<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <style type="text/css">
        *{margin: 0px;padding: 0px;}
        .box{
            position: absolute;
            height: 500px;
            width: 350px;
            border: 2px solid black;
            right: 0px;
            left: 0px;
            top: 0px;
            bottom: 0px;
            margin: auto auto;
        }
        input{
            width: 294px;
            height: 60px;
            padding: 10px;
            margin: 10px;
            line-height: 60px;
            font-size: 20px;
        }
        button{
            width: 100px;
            height: 40px;
            font-size: 18px;
            padding: 8px;
            margin: 5px;
            vertical-align: middle;
        }
       .box1-1,.box1-2,.box1-3,.box2-1{
           float:inline-start;
           margin: 0,auto;
       }
    style>
head>
<body>
   <div class="box">
    <div class="box1-1">
        <input type="text" id="inp" disabled/>
    div>
     <div class="box2-1">
        <button onclick="clr();">Clrbutton>
        <button onclick="back();">Delbutton>
        <button onclick="cal();">=button>
     div> 
    <div class="box1-2">
        <button onclick="addStr(1);">1button>
        <button onclick="addStr(2);">2button>
        <button onclick="addStr(3);">3button><br/>
        <button onclick="addStr(4);">4button>
        <button onclick="addStr(5);">5button>
        <button onclick="addStr(6);">6button><br/>
        <button onclick="addStr(7);">7button>
        <button onclick="addStr(8);">8button>
        <button onclick="addStr(9);">9button><br/>
        <button onclick="addStr(0);">0button>
        <button onclick="addStr('.');">.button>
        <button onclick="addStr('/');">/button><br/>
    div>
    <div class="box1-3">
        <button onclick="addStr('+');">+button>
        <button onclick="addStr('-');">-button>
        <button onclick="addStr('*');">*button>
    div>
    div>
    <script>
        function addStr(s){
            var inp=document.getElementById('inp');
            inp.value=inp.value+s;
        }
        function cal(){
            var inp=document.getElementById('inp');
            val=eval(inp.value);
            inp.value=val;
        }
        function clr(){//清除
            var inp=document.getElementById('inp');
            inp.value='';
        }
        function back(){//删除最后一个字符
            var inp=document.getElementById('inp');
            var str=inp.value.substr(0,inp.value.length-1);
            inp.value=str;
        }
    script>
body>
html>

JavaScript实现简单计算器(加减乘除)_第1张图片

JavaScript实现简单计算器(加减乘除)_第2张图片

你可能感兴趣的:(前端,#,JavaScript案例)