jQuery实现简单计算器功能
更新时间:2022年07月21日 10:49:18 作者:梦nine
这篇文章主要为大家详细介绍了jQuery实现简单计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud)
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
本文实例为大家分享了jQuery实现简单计算器的具体代码,供大家参考,具体内容如下
要求:
代码:
<html> <head> ? ? <meta charset="utf-8"> ? ? <title>我的计算器</title> ?? ?<script src="http://9i0i.com/pic.php?p=jquery-1.9.1.min.js"> </script> ? ? <script> ?? ? ? ? ? ? ? ? function myck(type) { ? ? ? ? ? ? var num1 = jQuery("#num1"); ? ? ? ? ? ? var num2 = jQuery("#num2"); ? ? ? ? ? ? var ?resultDiv=jQuery("#resultDiv"); ? ? ? ? ? ? if(type==1) { ? ? ? ? ? ? ? ? // 1.非空判断 ? ? ? ? ? ? ? ? if (num1.val() == "") { ? ? ? ? ? ? ? ? ? ? alert("请先输入数字1"); ? ? ? ? ? ? ? ? ? ? num1.focus(); ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (num2.val() == "") { ? ? ? ? ? ? ? ? ? ? alert("请先输入数字2"); ? ? ? ? ? ? ? ? ? ?num2.focus(); ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? // 2.加法操作 ? ? ? ? ? ? ? ? var total = parseInt(num1.val()) + parseInt(num2.val()); ? ? ? ? ? ? ? ? // 3.将结果展现在最下面 div 中 ? ? ? ? ? ? ? ? resultDiv.html( ? ? ? ? ? ? ? ? ? ? "<h2>最终执行结果:<strong style='color: red;'>" + total + "</strong></h2>"); ? ? ? ? ? ? }else if(type==2){ ? ? ? ? ? ? ? ? // 1.非空判断 ? ? ? ? ? ? ? if (num1.val() == "") { ? ? ? ? ? ? ? ? ? ? alert("请先输入数字1"); ? ? ? ? ? ? ? ? ? ? num1.focus(); ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (num2.val() == "") { ? ? ? ? ? ? ? ? ? ? alert("请先输入数字2"); ? ? ? ? ? ? ? ? ? ?num2.focus(); ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? // 2.减法操作 ? ? ? ? ? ? ? ? var sub = parseInt(num1.val()) - parseInt(num2.val()); ? ? ? ? ? ? ? ? // 3.将结果展现在最下面 div 中 ? ? ? ? ? ? ? ? resultDiv.html( ? ? ? ? ? ? ? ? ? ? "<h2>最终执行结果:<strong style='color: #ff0000;'>" + sub + "</strong></h2>"); ? ? ? ? ? ? }else if(type==3){ ? ? ? ? ? ? ? ? ?if (num1.val() == "") { ? ? ? ? ? ? ? ? ? ? alert("请先输入数字1"); ? ? ? ? ? ? ? ? ? ? num1.focus(); ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (num2.val() == "") { ? ? ? ? ? ? ? ? ? ? alert("请先输入数字2"); ? ? ? ? ? ? ? ? ? ?num2.focus(); ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? // 2.乘法操作 ? ? ? ? ? ? ? ? var mul = parseInt(num1.val()) * parseInt(num2.val()); ? ? ? ? ? ? ? ? // 3.将结果展现在最下面 div 中 ? ? ? ? ? ? ? ? resultDiv.html("<h2>最终执行结果:<strong style='color: red;'>" + mul+ "</strong></h2>"); ? ? ? ? ? ? } ? ? ? ? ? ? else if(type==4){ ?? ??? ??? ? ? ? ? ? ? ? ? ? ? ? num1.val(""); ? ? ? ? ? ? ? ? num2.val(""); ? ? ? ? ? ? ? ? resultDiv.html(""); ? ? ? ? ? ? } ? ? ? ? } ? ? </script> </head> <body> <div style="text-align: center;margin-top: 100px;"> ? ? <h1>计算器</h1> ? ? 数字1:<input id="num1" type="number"> <p></p> ? ? 数字2:<input id="num2" type="number"> <p></p> ? ? <div> ? ? ? ? <input type="button" value=" 加 法 " onclick="myck(1)"> ? ? ? ? <input type="button" value=" 减 法 " onclick="myck(2)"> ? ? ? ? <input type="button" value=" 相 乘 " onclick="myck(3)"> ? ? ? ? <input type="button" value=" 清 空 " onclick="myck(4)"> ? ? </div> ? ? <div id="resultDiv" style="margin-top: 50px;"> ? ? </div> </div> </body> </html>
效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。
相关文章
lyhucSelect基于Jquery的Select数据联动插件
lyhucSelect基于Jquery的Select数据联动插件,需要的朋友可以参考下。2011-03-03
最新评论