JavaScript —— 实现简单计算器【带有 开/关机 清零 退格 功能】
<!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;
box-shadow: 10px 10px #3d4a65;
margin: 40px auto;
padding: 40px 0px 30px 30px;
width: 200px;
} .btn {
outline: none;
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: 0px;
}
.btn, #display, form {
border-radius: 25px;
} .number {
background-color: #72778b;
box-shadow: 0 5px #5f6680;
color: #dededc;
}
.number:active {
box-shadow: 0 2px #5f6680;
-webkit-transform: translateY(2px);
-ms-transform: translateY(2px);
-moz-tranform: translateY(2px);
transform: translateY(2px);
}
.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{
outline: none;
border-radius: 25px;
background-color:#FF4500;
color:#FFFF00;
font-size:16px;
font-style:strong;
width:40px;
height:28px; }
.button_clear:active {
background-color: red;
box-shadow: 0 5px #666;
transform: translateY(2px);
}
.off_on{
outline: none;
border-radius: 25px;
background-color:#FF4500;
color:#FFFF00;
font-size:16px;
font-style:strong;
width:40px;
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(){
if(power == -1){
document.getElementById("display").value = "";
}else{
document.getElementById("display").value = "";
document.getElementById("display").focus();
}
};
function welcome(){
document.getElementById("display").value = "Welcome ☺";
setTimeout(welcome_end, 2000);
};
function welcome_end(){
document.getElementById("display").value = "";
document.getElementById("display").focus();
}; function get(value) {
if(power == 1){
document.getElementById("display").value += value;
}else{
document.getElementById("display").value = "";
} } ;
function back(){
if(power == 1){
var now_result = document.getElementById("display"); //*****************************
document.getElementById("display").value = now_result.value.substring(0, now_result.value.length - 1);
}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 开/关机\n按钮 B 退格");
};
</script>
</head>
<body link="grey" vlink="grey" alink="grey" Onload="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();">
<input type="button" class="off_on" value="B" onclick="back();">
<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>
JavaScript —— 实现简单计算器【带有 开/关机 清零 退格 功能】的更多相关文章
- JAVASCRIPT实现简单计算器
最终效果如下图-2,有bug:就是整数后点击%号结果正确,如果小数后面点击%的话结果就错误!其他都正常,求指点:input的value是string类型的,在JS中改如何正确处理下图-1中的if部分? ...
- 学习笔记之javascript编写简单计算器
感觉自己的的实力真的是有待提高,在编写计算器的过程中,出现了各种各样的问题,暴露了自己的基础不扎实,逻辑思维能力不够,学得知识不能运用到自己的demo中区.先介绍一些这个这个计算器的整体思路.大致 ...
- 用javascript编写的简单计算器
老师布置的任务,弄了一天,总算把代码搞出来了,作为菜鸟给自己一点小掌声,传说中JS是很简单的,但是要写出一个程序真的很需要锻炼,我的锻炼实在是太少了,天天DOTA中.呵呵(做了些小注释)大家应该能看懂 ...
- javascript简单计算器实践
参考部分资料,编写一个简单的计算器案例,虽然完成了正常需求,但是也有不满之处,待后续实力提升后再来补充,先把不足之处列出: 1:本来打算只要打开页面,计算器的输入框会显示一个默认为0的状态,但是在输入 ...
- JavaScript+HTML,简单的计算器实现
成功进化到程序猿快一年多了, 还没写过计算器, 正好今天比较闲,随手写了个计算器,最简单的实现,核心是eval()方法,把字符串作为JS代码处理,把输入的信息拼接成字符串,点等号执行代码得到结果,出异 ...
- 用JavaScript制作简单的计算器
<html > <head> <title>简单计算器</title> <style type="text/css"> ...
- Linux——系统开关机指令简单学习笔记
关机: 命令名称:shutdown 命令所在路径:/usr/sbin/shutdown 执行权限:root 语法:shutdown 功能描述:关机 范例:# shutdown -h now 重启: 命 ...
- 利用css和javascript实现简单的计算器
<!doctype html> <html> <head> <!--声明当前页面的编码集--> <meta http-equiv="Co ...
- 菜鸟学Android编程——简单计算器《一》
菜鸟瞎搞,高手莫进 本人菜鸟一枚,最近在学Android编程,网上看了一些视频教程,于是想着平时手机上的计算器应该很简单,自己何不尝试着做一个呢? 于是就冒冒失失的开撸了. 简单计算器嘛,功能当然很少 ...
随机推荐
- 利用logrotate切割nginx的access.log日志
一.新建一个nginx的logrotate配置文件 /var/log/nginx/access.log { daily rotate compress delaycompress missingok ...
- Django学习系列8:django测试客户端
"""向浏览器返回真正的HTML响应,添加一个新的测试方法""" from django.test import TestCase from ...
- x64工程属性 选择切换不了
删除x64工程属性 重新建立新的 配置属性 和导入工程类似 属性冲突 最好重新建立新的工程 解决
- python打开文件的方式
r 以只读模式打开文件 w 以只写模式打开文件,文件若存在,首先要清空,然后(重新创建) a 以追加模式打开(从EOF开始,必要时创建新文件),把所有要写入文件的数据追加到文件的末尾,即使使 ...
- java modCount和fail-fast
在迭代遍历线程不安全的集合的时候,如ArrayList,如果其他线程修改了该集合,那么将抛出ConcurrentModificationException,这就是 fail-fast 策略. modC ...
- 3.Pod控制器应用进阶
一.Pod的生命周期 init container -- Post start -- running -- pre stop -- main container 创建Pod经历的过程:->a ...
- 【leetcode】Global and Local Inversions
题目如下: We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. The number of (g ...
- 如何导出不带.svn的文件夹
在工作环境中,有的时候需要将本地SVN服务器中的文件导出来,提交到另一个SVN服务器中去(比如做现场开发时,由于外网速度慢,项目组内部往往使用一个SVN服务器,但又同时又需要公司统一管理,定期提交到公 ...
- linux lazarus 连接mssqlserver
1 . 从https://www.freetds.org/ 下载驱动源文件 2. 参照 https://www.freetds.org/userguide/config.htm 内容编译 3. 启动l ...
- linux-包管理器-4
安装 升级 查询 导入公钥 rpm -K|checksig rpmfile 检查包的完整性和签名 rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 ...