ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JavaScript —— 实现简单计算器【带有 开/关机 清零 功能】

2019-06-20 15:45:31  阅读:298  来源: 互联网

标签:document color JavaScript value getElementById background 计算器 清零 display


 

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="keywords" content="Calculator">
    <meta name="description" content="Calculator">
    <meta name="author" content="LIEGE">
    <title>Calculator</title>
    
<style>

* {
    border: none;
    margin: 0;
    padding: 0;
}
body {

}
.center {
    background-color: #fff;
    border-radius: 50%;
    height: 800px;
    margin: auto;
    width: 800px;
}
h1 {
    color: #495678
    font-size: 30px;    
    margin-top: 20px;
    padding-top: 50px;
    display: block;
    text-align: center;
    text-decoration: none;
}

form {
    background-color: #495678;
    margin: 40px auto;
    padding: 40px 0px 30px 30px;    
    width: 200px;
}

.btn {
    font-size: 20px;
    height: 45px;
    margin: 5px 0 5px 10px;
    width: 45px;
}
.btn:first-child {
    margin: 5px 0 5px 10px;
}


#display {
    outline: none;
    background-color: white;
    color: black;
    font-size: 20px;
    height: 47px;
    text-align: right;
    width: 165px;
    padding-right: 10px;
    margin-left: 10px;
}
.number {
    background-color: #72778b;
    box-shadow: 0 5px #5f6680;
    color: #dededc;
}
.number:hover {
    background-color: rgb(114,119,139,0.4);
    box-shadow: 0 5px #5f6680;
    color: #dededc;
}

.operator {
    background-color: #dededc;
    color: #72778b;
}

.operator:hover {
    background-color:rgb(222,222,220,0.6);
    color: #72778b;
}

.other {
    background-color:rgb(255,0,0,0.7);
    color:#dededc ;
}
.other:hover {
    background-color:rgb(255,0,0,0.4);
    color:#dededc ;
}


.button_clear{
    background-color:#FF4500;
    color:#FFFF00;
    font-size:16px;
    font-style:strong;
    width:28px;
    height:28px;

}
.button_clear:active {
  background-color: red;
  box-shadow: 0 5px #666;
  transform: translateY(2px);
}
.off_on{
    background-color:#FF4500;
    color:#FFFF00;
    font-size:16px;
    font-style:strong;
    width:35px;
    height:28px;
}
.off_on:active{
    background-color: red;
    box-shadow: 0 5px #666;
    transform: translateY(2px);
}
.tishi{
    font-size:10px;
    text-decoration:none;
}
</style>
<script>
var power = 1;
// 1 是开机   -1是关机
function off(){//一开始就运行---》 关机
    power = -power;
    if(power == -1){
        document.getElementById("display").value = "";
    }else{
        welcome();
    }

};
function clearout(){
document.getElementById("display").value = "";

};
function welcome(){
document.getElementById("display").value = "Welcome ☺";
setTimeout(welcome_end, 2000);
};
function welcome_end(){
document.getElementById("display").value = "";
};


function get(value) {
    if(power == 1){
        document.getElementById("display").value += value; 
    }else{
        document.getElementById("display").value = "";
    }
    
    
} ;
function calculates() {
    if(power == 1){
        var result = 0;
        result = document.getElementById("display").value;
        document.getElementById("display").value = "";
        document.getElementById("display").value = eval(result);
    }else{
        document.getElementById("display").value = "";
    }
    
    
    
};
function tishi(){
confirm("按钮 C 清零  \n按钮 OFF 开/关机");
};
</script>
</head>
<body  link="grey" vlink="grey" alink="grey" onl oad="off()">
    <div class="center">
        <h1 >Unbrealla™ Calculator <a href="#" onclick="tishi()" class="tishi">HELP</a></h1>
        <form name="calculator">
            
            
            <input type="text" id="display" width="300px" value="">
                <br>
                    <br><input type="button" class="button_clear" value="C" onclick="clearout();">
                        <input type="button" class="off_on" value="OFF" onclick="off();">
                <br>
            <input type="button" class="btn number" value="1" onclick="get(this.value);">
            <input type="button" class="btn number" value="2" onclick="get(this.value);">
            <input type="button" class="btn number" value="3" onclick="get(this.value);">
            <br />
            <input type="button" class="btn number" value="4" onclick="get(this.value);">
            <input type="button" class="btn number" value="5" onclick="get(this.value);">
            <input type="button" class="btn number" value="6" onclick="get(this.value);">
            <br />
            <input type="button" class="btn number" value="7" onclick="get(this.value);">
            <input type="button" class="btn number" value="8" onclick="get(this.value);">
            <input type="button" class="btn number" value="9" onclick="get(this.value);">
            <br />
            
            <input type="button" class="btn number" value="0" onclick="get(this.value);">
            <input type="button" class="btn operator" value="-" onclick="get(this.value);">
            <input type="button" class="btn operator" value="+" onclick="get(this.value);">
            <br />
            
            <input type="button" class="btn operator" value="*" onclick="get(this.value);">
            <input type="button" class="btn operator" value="/" onclick="get(this.value);">        
            <input type="button" class="btn other" value="=" onclick="calculates();">
        </form>
    </div>
    
</body>
</html>

 

标签:document,color,JavaScript,value,getElementById,background,计算器,清零,display
来源: https://www.cnblogs.com/expedition/p/11058981.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有