thinkPHP框架实现的简单计算器示例

HTML部分 文件名 index.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>计算器</title> <script type="text/javascript" src="https://www.jb51.net/public/js/jquery-2.2.3.js"></script> <link href="https://www.jb51.net/public/css/index.css" > </head> <body> <div> <span>登录</span> </div> <div> <form action=""> <span>请输入你的手机号开始使用</span> <br><br> <input type="text"> <input type="button" value="使用"> <br><br> </form> </div> <div> <div> <span>计算器</span> <br><br> <input type="text"> <br><br> <form action=""> <br><br> <input type="button" value="1"> <input type="button" value="2"> <input type="button" value="3"> <input type="button" value="4"> <input type="button" value="5"> <br><br> <input type="button" value="6"> <input type="button" value="7"> <input type="button" value="8"> <input type="button" value="9"> <input type="button" value="0"> <br><br> <input type="button" value="+"> <input type="button" value="-"> <input type="button" value="*"> <input type="button" value="https://www.jb51.net/"> <input type="button" value="="> <br><br> <input type="button" value="."> <input type="button" value="←"> <input type="button" value="c"> <input type="button" value="CE"> <input type="button" value="MC"> </form> </div> <div> <div> <span>当前结果:</span><span></span> </div> <br><br> <span>历史记录:</span> <ul> <li><span>删除</span></li> </ul> </div> </div> </body> <script type="text/javascript" src="https://www.jb51.net/public/js/index.js"> </script> </html>

CSS样式 文件名 index.css

.login{/*登录*/ height: 30px; width: 100px; background-color: #00a2d4; text-align: center; cursor: pointer; padding-top: 10px; position: fixed; } .register{ display: none; position: fixed; } .calculator{ display: none; position: fixed; } .counter{ border: 1px solid black; height: 400px; width: 320px; float: left; } .import{ height: 20px; width: 180px; margin-top: 50px; margin-left: 50px; } .snap{ margin-left: 50px; margin-top: -30px; } .snap input{ height: 30px; width: 30px; } .result{ border: 1px solid black; height: 400px; width: 320px; float: left; margin-left: 50px; } .brand{ position: relative; top: 50px; left: 90px; }

JS部分  文件名 index.js

//计算屏幕宽高 var w_width = $(window).width(); var w_height = $(window).height(); var operator = 0;//运算符号 var change = 0;//属于运算符后需要清空上一数值 var num1 = 0;//元算的第一个数据 var num2 = 0;//运算的第二个数据 var sum = 0;//运算结果 //居中 function setCenter(obj){ var this_width = $(obj).width(); var this_height = $(obj).height(); var this_left = parseInt((w_width-this_width)/2); var this_height = parseInt((w_height-this_height)/2); $(obj).css({left:this_left,top:this_height}); } //正则 function testReg() { //定义参数 var regType = arguments[0]?arguments[0]:'phone'; var myString = arguments[1]?arguments[1]:false; var regArray = new Array(); regArray['phone'] = /^1[3|4|5|7|8]\d{9}$/; regArray['email'] = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;//邮箱 *代表{0,} +代表{1,} ?代表{0,1} //判断 if (myString){ return regArray[regType].test(myString); } return false; } //发送数据到后台 function sendMessage() { //手机号 var myphone = $("#myphone").val(); //计算器 var myUrl = '/app/base.php'; var myData = {num1:num1,num2:num2,cal_option:operator,cal_result:sum,myphone:myphone,type:'add'}; $.post(myUrl,myData,function (msg) { //TODO },'json') getResultByPhone(); } //获取结果 function getResultByPhone() { var myphone = $("#myphone").val(); var myUrl = '/app/base.php'; var myData = {myphone:myphone,type:'getResult'}; $.post(myUrl,myData,function (msg) { //TODO $("#cal_result").html(msg); },'text') } //获取数据 function deleteHistory(id) { var myUrl = '/app/base.php'; var MyData = {id:id,type:'delete'}; $.post(myUrl,MyData,function (msg) { //TODO },'json') getResultByPhone(); } $(function () { //登录居中 setCenter($(".login").show(8000)); //点击登录显示输入 $(".login").click(function(){ setCenter($(".register").show()); $(this).hide(); }); //点击使用显示计算器 $("#use").click(function () { if (testReg('phone',$("#myphone").val())){ setCenter($(".calculator").show()); $(".register").hide(); getResultByPhone() }else { alert ("你输的手机格式不对"); return false; } }) $(".order").click(function () {//点击数字 var num = $(this).val(); var oldValue = $(".import").val(); if (change ==1){ oldValue = "0"; change = 0; } var newValue = ""; if (num == -1){ oldValue = parseFloat(oldValue); newValue = oldValue * -1; }else if (num == "."){ if (oldValue.indexOf('.') == -1) newValue = oldValue + "."; else newValue = oldValue; }else { if (oldValue == 0 && oldValue.lastIndexOf('.') == -1){ newValue = num; }else { newValue = oldValue + num; } } $(".import").val(newValue); }); $("#clear").click(function () {//清除 $(".import").val("0"); operator = 0; change = 0; num1 = 0; num2 = 0; }); $("#backspace").click(function () {//退格 if (change ==1){ operator = 0; change = 0; } var value = $(".import").val(); if (value.length == 1){ $(".import").val("0"); }else { value = value.substr(0,value.length - 1); $(".import").val(value); } }); $(".operator").click(function() {//点击运算符号触发事件 change = 1; operator = $(this).val(); var value = $(".import").val(); var dianIndex = value.indexOf("."); if (dianIndex == value.length) { value = value.substr(0, value.length - 1); } num1 = parseFloat(value); }); $("#equal").click(function () {//点击等号 var value = $(".import").val(); var dianIndex = value.indexOf("."); if (dianIndex == value.length){ value = value.substr(0,value.length - 1); } var equal = $(this).val(); num2 = parseFloat(value); if (operator == "+"){ sum = num1 + num2; }else if (operator == "-"){ sum = num1 - num2; }else if (operator == "*"){ sum = num1 * num2; }else if (operator == "https://www.jb51.net/"){ sum = num1 / num2; }else if (operator == "" || num1 ==0 || num2 == 0){ sum = num1 +num2; } var re = /^[0-9]+.?[0-9]*$/; if (re.test(sum)){ sum = sum.toFixed(2); } $(".import").val(sum); sendMessage(); $("#current_results").text(num1 + operator + num2 + equal + sum); change = 1; operator = 0; num1 = 0; num2 = 0; }); })

接口 文件名 IDB.php

<?php namespace mao; interface IDB{ public function insert($data); public function update($data); public function select($data); public function del($data); }

创建一个Mysqli类继承接口实现增删改查

文件名 MySqli.clsaa.php

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/595dae8475188d8466ef94d0f65e1a01.html