JavaScript简易计算器
JavaScript一种直译式脚本语言,是一种动态类型、弱类型、基于原型的语言,内置支持类型。它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标准通用标记语言下的一个应用)网页上使用,用来给HTML网页增加动态功能。
ECMAScript,描述了该语言的语法和基本对象。
- 是一种解释性脚本语言(代码不进行预编译)。
- 主要用来向HTML(标准通用标记语言下的一个应用)页面添加交互行为。
- 可以直接嵌入HTML页面,但写成单独的js文件有利于结构和行为的分离。
- 跨平台特性,在绝大多数浏览器的支持下,可以在多种平台下运行(如Windows、Linux、Mac、Android、iOS等)。
直接上代码
html文件代码:
<html> <head>
<meta charset="utf-8">
<link href="计算器.css" rel="stylesheet">
</head> <body>
<div id="big">
<div id="top">
<span id="title">JavaScript计算器</span>
<span id="author">@温一壶清酒</span>
</div> <div id="import">
<div id="data">
<input type="text" id="dataname">
</div>
</div> <div id="key">
<input type="button" id="CE" onclick="clearnum()" value="CE" class="buttons">
<input type="button" id="C" onclick="clearnum()" value="C" class="buttons">
<input type="button" id="Back" onclick="back()" value="Back" class="buttons">
<input type="button" id="/" onclick="calc(this.id)" value="/" class="buttons" style="margin-right:0px"> <input type="button" id="7" onclick="calc(this.id)" value="7" class="buttons">
<input type="button" id="8" onclick="calc(this.id)" value="8" class="buttons">
<input type="button" id="9" onclick="calc(this.id)" value="9" class="buttons">
<input type="button" id="*" onclick="calc(this.id)" value="*" class="buttons" style="margin-right:0px"> <input type="button" id="4" onclick="calc(this.id)" value="4" class="buttons">
<input type="button" id="5" onclick="calc(this.id)" value="5" class="buttons">
<input type="button" id="6" onclick="calc(this.id)" value="6" class="buttons">
<input type="button" id="-" onclick="calc(this.id)" value="-" class="buttons" style="margin-right:0px"> <input type="button" id="1" onclick="calc(this.id)" value="1" class="buttons">
<input type="button" id="2" onclick="calc(this.id)" value="2" class="buttons">
<input type="button" id="3" onclick="calc(this.id)" value="3" class="buttons">
<input type="button" id="+" onclick="calc(this.id)" value="+" class="buttons" style="margin-right:0px"> <input type="button" id="±" onclick="calc(this.id)" value="±" class="buttons">
<input type="button" id="0" onclick="calc(this.id)" value="0" class="buttons">
<input type="button" id="." onclick="calc(this.id)" value="." class="buttons">
<input type="button" id="=" onclick="eva()" value="=" class="buttons" style="margin-right:0px">
</div> <div id="bottom">
<span id="welcome">欢迎使用JavaScript计算器</span>
</div> </div>
<script src="计算器.js"></script> </body> </html>
css样式代码:
*{
margin:;
padding:;
box-sizing: border-box;
font: 14px Arial,sans-serif;
}
html{
height:100%;
background-color:lightslategrey;
}
#big{
margin: 15px auto;
width:330px;
height:470px;
background-color:darkgrey;
border: 1px solid lightgray;
padding:15px;
}
#top{
height:20px;
}
#title{
float:left;
line-height:30px;
}
#author{
float:right;
line-height:30px;
}
#import{
margin-top:15px;
}
#dataname{
margin-top:5px;
width:300px;
height:40px;
text-align:right;
padding-right:10px;
font-size:20px;
}
#key{
border:1px solid lightgray;
height:293px;
margin-top:25px;
padding:16px;
}
.buttons{
float:left;
width:52px;
height:36px;
text-align:center;
background-color:lightgray;
margin:0 18px 20px 0;
}
.buttons:hover{
color:white;
background-color:blue;
}
#bottom{
margin-top:20px;
height:20px;
text-align:center;
}
js代码:
var number = 0; // 定义第一个输入的数据
function calc(number) {
//获取当前输入
if(number=="%"){
document.getElementById('dataname').value=Math.round(document.getElementById('dataname').value)/100;
}else{
document.getElementById('dataname').value += document.getElementById(number).value;
}
}
function eva() {
//计算输入结果
document.getElementById("dataname").value = eval(document.getElementById("dataname").value);
}
function clearnum() {
//清零
document.getElementById("dataname").value = null;
document.getElementById("dataname").focus();
}
function back() {
//退格
var arr = document.getElementById("dataname");
arr.value = arr.value.substring(0, arr.value.length - 1);
}
JavaScript简易计算器的更多相关文章
- 自己做的javascript简易计算器
html <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF- ...
- JavaScript之简易计算器
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...
- 前端 JavaScript 实现一个简易计算器
前端使用 JavaScript 实现一个简易计算器,没有难度,但是里面有些小知识还是需要注意的,算是一次基础知识回顾吧. 题目 实现一个简易版的计算器,需求如下: 1.除法操作时,如果被除数为0,则结 ...
- 剖析简易计算器带你入门微信小程序开发
写在前面,但是重点在后面 这是教程,也不是教程. 可以先看Demo的操作动图,看看是个什么玩意儿,GitHub地址(https://github.com/dunizb/wxapp-sCalc) 自从微 ...
- 使用HTML+CSS,jQuery编写的简易计算器
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 使用HTML+CSS,jQuery编写的简易计算器后续(添加了键盘监听)
之前发布了一款简易的计算器,今天做了一下修改,添加了键盘监听事件,不用再用鼠标点点点啦 JS代码: var yunSuan = 0;// 运算符号,0-无运算;1-加法;2-减法;3-乘法;4-除法 ...
- 使用html+css+js实现简易计算器
使用html+css+js实现简易计算器, 效果图如下: html代码如下: <!DOCTYPE html> <html lang="en"> <he ...
- 微信小程序-简易计算器
代码地址如下:http://www.demodashi.com/demo/14210.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...
- 用js制作简易计算器及猜随机数字游戏
<!doctype html><html><head> <meta charset="utf-8"> <title>JS ...
随机推荐
- 【渗透课程】前言-揭开Web渗透与安全的面纱(必看)
服务器是怎么被入侵的 攻击者想要对一台计算机渗透必须具备以下条件: 1.服务器与客户端能够正常通讯 (服务器是为客户端提供服务的) 2.服务器向客户端提供的权限(服务)或者说是端口. 服务端所提供的服 ...
- 第一章 [分布式CMS]
第一次准备写一个完整的例子, 也是正在做的一个项目! 准备记录一下"心路历程".... 项目为分布式的CMS 前期架构分为,单点登录,结构系统,资源云,文档云 ,DB服务,工具服务 ...
- [2012-06-18]awk利用关联数组合并记录
问题源起:http://bbs.chinaunix.net/thread-3753784-1-1.html 代码如下 {% capture text %} $awk '{if(!a[$1]){a[$1 ...
- Java 随笔记录
1. java对象转json Message msg = generateMessage();ObjectMapper mapper = new ObjectMapper();String json ...
- C++与类型转换相关的四个关键字及其特点
1.reinterpret_cast (expression) type-id 必须是一个指针.引用.算术类型.函数指针或者成员指针. 它可以把一个指针转换成一个整数,也可以把一个整数转换成一个指针( ...
- svn status详解
svn 是在提交前查看本地文本和版本库里面的文件的区别.返回值有许多种具体含义如下: [url=] L abc.c # svn已经在.svn目录锁定了abc.c M ...
- 用CAS方案解决高并发一致性问题
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt395 缘起:在高并发的分布式环境下,对于数据的查询与修改容易引发一致性问题, ...
- 解决MVC模式文件下载附件中文名称乱码
解决如下: 进行url编码:Server.UrlPathEncode(file.AttachmentName) return File(file.TempWorkPath, CommonTools.G ...
- java常见面试题(二)
1.java集合类 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).JavaSDK不提供直接继承自Collect ...
- eclipse+git
最近朋友都推荐使用github管理自己的项目,而且免费用户可以有5个仓库,恰好我也想了解下git,借此机会学习一下.github官方指南使用独立第三方git工具来进行版本控制,并不借助于eclipse ...