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 ...
随机推荐
- [知了堂学习笔记]_JSON数据操作第1讲(初识JSON)
一.认识JSON 什么是JSON? JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式..它基于 ECMAScript (w3c制定的js规 ...
- java‘小秘密’系列(三)---HashMap
java'小秘密'系列(三)---HashMap java基础系列 java'小秘密'系列(一)---String.StringBuffer.StringBuilder java'小秘密'系列(二)- ...
- Tornado模板
--------------------静态文件-------------------- 1.static_path:通过向web.Application类的构造函数传递一个名为static_path ...
- 字符编码知识简介和iconv函数的简单使用
字符编码知识简介和iconv函数的简单使用 字符编码知识简介 我们知道,在计算机的世界其实只有0和1.期初计算机主要用于科学计算,而我们知道一个数,除了用我们常用对10进制表示,也可以用2进制表示,所 ...
- h5audio标签
因为音频格式有版权,各浏览器使用不同的音频格式. 音频格式兼容性 音频格式 Chrome Firefox IE9 Opera Safari MP3 支持 不支持 支持 不支持 支持 OGG 支持 支持 ...
- input[type="button"]与<button>的区别
<button>标签 浏览器支持 所有主流浏览器都支持<button>标签. 重要事项:如果在HTML表单中使用button元素,不同的浏览器会提交不同的值.IE将提交& ...
- C# 导出Excel的示例(转)
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.W ...
- vim插件ctags的安装与使用
LINUX系统下看程序或者编程序时,看到一个函数经常需要知道该函数的定义,这时ctags就派上用场了,其安装和使用方法如下: 安装方法: sudo apt-get install ctags (ubu ...
- poj 1384完全背包
题意:给出猪罐子的空质量和满质量,和n个硬币的价值和质量,求猪罐子刚好塞满的的最小价值. 思路:选择硬币,完全背包问题,塞满==初始化为无穷,求最小价值,min. 代码: #include<io ...
- 关于C++中计时的方法
在C++的库函数中,我们可以使用clock()来计算程序的运行时间,主要使用一下三个函数类型及函数: 1.clock_t:数据类型,其实,当你打开time.h就知道了,就是个long型,用来记录一段时 ...