jQuery實現(xiàn)簡單計算器功能
更新時間:2022年07月21日 10:49:18 作者:夢nine
這篇文章主要為大家詳細介紹了jQuery實現(xiàn)簡單計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了jQuery實現(xiàn)簡單計算器的具體代碼,供大家參考,具體內容如下
要求:
代碼:
<html> <head> ? ? <meta charset="utf-8"> ? ? <title>我的計算器</title> ?? ?<script src="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("請先輸入數(shù)字1"); ? ? ? ? ? ? ? ? ? ? num1.focus(); ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (num2.val() == "") { ? ? ? ? ? ? ? ? ? ? alert("請先輸入數(shù)字2"); ? ? ? ? ? ? ? ? ? ?num2.focus(); ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? // 2.加法操作 ? ? ? ? ? ? ? ? var total = parseInt(num1.val()) + parseInt(num2.val()); ? ? ? ? ? ? ? ? // 3.將結果展現(xiàn)在最下面 div 中 ? ? ? ? ? ? ? ? resultDiv.html( ? ? ? ? ? ? ? ? ? ? "<h2>最終執(zhí)行結果:<strong style='color: red;'>" + total + "</strong></h2>"); ? ? ? ? ? ? }else if(type==2){ ? ? ? ? ? ? ? ? // 1.非空判斷 ? ? ? ? ? ? ? if (num1.val() == "") { ? ? ? ? ? ? ? ? ? ? alert("請先輸入數(shù)字1"); ? ? ? ? ? ? ? ? ? ? num1.focus(); ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (num2.val() == "") { ? ? ? ? ? ? ? ? ? ? alert("請先輸入數(shù)字2"); ? ? ? ? ? ? ? ? ? ?num2.focus(); ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? // 2.減法操作 ? ? ? ? ? ? ? ? var sub = parseInt(num1.val()) - parseInt(num2.val()); ? ? ? ? ? ? ? ? // 3.將結果展現(xiàn)在最下面 div 中 ? ? ? ? ? ? ? ? resultDiv.html( ? ? ? ? ? ? ? ? ? ? "<h2>最終執(zhí)行結果:<strong style='color: #ff0000;'>" + sub + "</strong></h2>"); ? ? ? ? ? ? }else if(type==3){ ? ? ? ? ? ? ? ? ?if (num1.val() == "") { ? ? ? ? ? ? ? ? ? ? alert("請先輸入數(shù)字1"); ? ? ? ? ? ? ? ? ? ? num1.focus(); ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (num2.val() == "") { ? ? ? ? ? ? ? ? ? ? alert("請先輸入數(shù)字2"); ? ? ? ? ? ? ? ? ? ?num2.focus(); ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? // 2.乘法操作 ? ? ? ? ? ? ? ? var mul = parseInt(num1.val()) * parseInt(num2.val()); ? ? ? ? ? ? ? ? // 3.將結果展現(xiàn)在最下面 div 中 ? ? ? ? ? ? ? ? resultDiv.html("<h2>最終執(zhí)行結果:<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> ? ? 數(shù)字1:<input id="num1" type="number"> <p></p> ? ? 數(shù)字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>
效果:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
基于jQuery Tipso插件實現(xiàn)消息提示框特效
這篇文章主要介紹了基于jQuery Tipso插件實現(xiàn)消息提示框特效,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03jQuery中的pushStack實現(xiàn)原理和應用實例
這篇文章主要介紹了jQuery中的pushStack實現(xiàn)原理和應用實例,pushStack是jQuery內核中一個非常重要的函數(shù),許多jQuery內部函數(shù)中都頻繁用到它,掌握這個函數(shù),有利于理解jQuery的運行原理,需要的朋友可以參考下2015-02-02JSONP 跨域訪問代理API-yahooapis實現(xiàn)代碼
介紹一個JSONP 跨域訪問代理API-yahooapis,需要的朋友可以參考下2012-12-12