javascript 实现简单计算器

文章目录

    • 效果图:
    • css代码
    • html代码
    • js代码

效果图:

javascript 实现简单计算器_第1张图片

css代码

	body {
		display: flex;
		justify-content: center;
		align-items: center;
		min-height: 100vh;
		background-color: #dcf3f3;
	}
	div{
		margin: auto;
		border: 1px solid orange;
		overflow: hidden;
		font-size: 30px;
		border-radius: 10px;
	}
	.div4:active{
		background-color: pink;
	}
	#div1{
		width: 540px;
	}
	#div2{
		width: 100%;
		height: 90px;
	}
	.div3{
		width: 268px;
		height: 80px;
		line-height: 80px;
		text-align: center;
		float: left;
		background-color: #e5eaff;
		
	}
	.div4{
		width: 113px;
		height: 80px;
		line-height: 80px;
		text-align: center;
		float: left;
		margin: 10px;
		border-radius: 50%;
		background-color: #F0FFFF;
	}
	#txt{
		width: 535px;
		height: 60px;
		border: 1px #E5EAFF;
		text-align: right;
		font-size: 30px;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: #f6fffb;
	}
	#txtresult{
		width: 535px;
		height: 30px;
		border: 1px #E5EAFF;
		text-align: right;
		font-size: 30px;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: #f6fffb;
	}
	span{
		display: inline-block;
		width: 100%;
		height: 100%;
        cursor: pointer;
	}

html代码

DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>计算器title>
		<link rel="stylesheet" type="text/css" href="./css/Mystyle.css"/>
		<script src="./js/MyStyle.js" type="text/javascript" charset="utf-8">script>
	head>
	<body>
		<div id="div1">
			<div id="div2">
				<input type="text" readonly="readonly" id="txt"  />
				<input type="text" readonly="readonly" name="" id="txtresult"  />
			div>
			<div class="div3">
				<span id='del'>
					del
				span>
			div>
			<div class="div3">
				<span id='clear'>clearspan>
			div>
			<div class="div4">
				<span id='7'>7span>
			div>
			<div class="div4">
				<span id='8'>8span>
			div>
			<div class="div4">
				<span id='9'>9span>
			div>
			<div class="div4">
				<span id='/'>/span>
			div>
			<div class="div4">
				<span id='4'>4span>
			div>
			<div class="div4">
				<span id='5'>5span>
			div>
			<div class="div4">
				<span id='6'>6span>
			div>
			<div class="div4">
				<span id='*'>*span>
			div>
			<div class="div4">
				<span id='1'>1span>
			div>
			<div class="div4">
				<span id='2'>2span>
			div>
			<div class="div4">
				<span id='3'>3span>
			div>
			<div class="div4">
				<span id='-'>-span>
			div>
			<div class="div4">
				<span id='0'>0span>
			div>
			<div class="div4">
				<span id='.'>.span>
			div>
			<div class="div4">
				<span id='='>=span>
			div>
			<div class="div4">
				<span id='+'>+span>
			div>
		div>
	body>
html>

js代码

window.onload = function(){
	var one = document.getElementById('1');
	var two = document.getElementById('2');
	var there = document.getElementById('3');
	var four = document.getElementById('4');
	var fives = document.getElementById('5');
	var six = document.getElementById('6');
	var Seven = document.getElementById('7');
	var eight = document.getElementById('8');
	var nine = document.getElementById('9');
	var zero = document.getElementById('0');
	var point = document.getElementById('.');
	var chu = document.getElementById('/');
	var cheng = document.getElementById('*');
	var jian = document.getElementById('-');
	var jia = document.getElementById('+');
	var del = document.getElementById('del');
	var clear = document.getElementById('clear');
	var result = document.getElementById('txt');
	var denyu = document.getElementById('=');
	var results = document.getElementById('txtresult');
	one.onclick = function(){
		if(results.value != ''){
			results.value = '';
		}
		result.value = result.value+1;
	}
	two.onclick = function(){
		if(results.value != ''){
			results.value = '';
		}
		result.value = result.value+2;
	}
	there.onclick = function(){
		if(results.value != ''){
			results.value = '';
		}
		result.value = result.value+3;
	}
	four.onclick = function(){
		if(results.value != ''){
			results.value = '';
		}
		result.value = result.value+4;
	}
	fives.onclick = function(){
		if(results.value != ''){
			results.value = '';
		}
		result.value = result.value+5;
	}
	six.onclick = function(){
		if(results.value != ''){
			results.value = '';
		}
		result.value = result.value+6;
	}
	Seven.onclick = function(){
		if(results.value != ''){
			results.value = '';
		}
		result.value = result.value+7;
	}
	eight.onclick = function(){
		if(results.value != ''){
			results.value = '';
		}
		result.value = result.value+8;
	}
	nine.onclick = function(){
		if(results.value != ''){
			results.value = '';
		}
		result.value = result.value+9;
	}
	zero.onclick = function(){
		if(results.value != ''){
			results.value = '';
		}
		result.value = result.value+0;
	}
	point.onclick = function(){
		if(results.value != ''){
			results.value = '';
		}
		//如果用户连续点击运算符,则以最后的为准
		if(result.value.substring(result.value.length-1)=='.'||result.value.substring(result.value.length-1)=='+'||result.value.substring(result.value.length-1)=='-'||result.value.substring(result.value.length-1)=='*'||result.value.substring(result.value.length-1)=='/'){
			result.value = result.value.substring(0,result.value.length-1)+'.';
			return;
		}
		result.value = result.value+'.';
	}
	chu.onclick = function(){
		if(result.value == ''){
			alert('错误!');
			return;
		}
		if(result.value.substring(result.value.length-1)=='.'||result.value.substring(result.value.length-1)=='+'||result.value.substring(result.value.length-1)=='-'||result.value.substring(result.value.length-1)=='*'||result.value.substring(result.value.length-1)=='/'){
			result.value = result.value.substring(0,result.value.length-1)+'/';
			return;
		}
		result.value = result.value+'/';
	}
	cheng.onclick = function(){
		if(result.value == ''){
			alert('错误!');
			return;
		}
		if(result.value.substring(result.value.length-1)=='.'||result.value.substring(result.value.length-1)=='+'||result.value.substring(result.value.length-1)=='-'||result.value.substring(result.value.length-1)=='*'||result.value.substring(result.value.length-1)=='/'){
			result.value = result.value.substring(0,result.value.length-1)+'*';
			return;
		}
		result.value = result.value+'*';
	}
	jian.onclick = function(){
		if(result.value == ''){
			alert('错误!');
			return;
		}
		if(result.value.substring(result.value.length-1)=='.'||result.value.substring(result.value.length-1)=='+'||result.value.substring(result.value.length-1)=='-'||result.value.substring(result.value.length-1)=='*'||result.value.substring(result.value.length-1)=='/'){
			result.value = result.value.substring(0,result.value.length-1)+'-';
			return;
		}
		result.value = result.value+'-';
	}
	jia.onclick = function(){
		if(result.value == ''){
			alert('错误!');
			return;
		}
		if(result.value.substring(result.value.length-1)=='.'||result.value.substring(result.value.length-1)=='+'||result.value.substring(result.value.length-1)=='-'||result.value.substring(result.value.length-1)=='*'||result.value.substring(result.value.length-1)=='/'){
			result.value = result.value.substring(0,result.value.length-1)+'+';
			return;
		}
		result.value = result.value+'+';
	}
	clear.onclick = function(){
		result.value = '';
		results.value = '';
	}
	del.onclick = function(){
		result.value = result.value.substring(0,result.value.length-1);
	}
	denyu.onclick = function(){
		if(result.value == ''){
			alert("温馨提示:请输入操作数!!");
			result.focus();
			return;
		}
		//定义一个变量存储最终结果
		var answer = '';
		//判断
		if(result.value.substring(result.value.length-1)=='.'||result.value.substring(result.value.length-1)=='+'||result.value.substring(result.value.length-1)=='-'||result.value.substring(result.value.length-1)=='*'||result.value.substring(result.value.length-1)=='/'){
			result.focus();
			alert("温馨提示:请输入完整的表达式!!");
			return;
		}
		
		//计算
		var answer = eval(result.value);
		results.value = answer;
		result.value = '';
	}
	
}

你可能感兴趣的:(前端,javascript,css,css3)