網站首頁 編程語言 正文
本文實例為大家分享了jQuery實現簡單計算器的具體代碼,供大家參考,具體內容如下
要求:
代碼:
<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("請先輸入數字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>
效果:
原文鏈接:https://blog.csdn.net/qq_54850622/article/details/115268060
相關推薦
- 2023-04-27 React中關于render()的用法及說明_React
- 2022-09-02 Python中的Numpy?面向數組編程常見操作_python
- 2022-04-03 Nginx構建Tomcat集群的操作方法_nginx
- 2022-09-19 LyScript尋找ROP漏洞指令片段的方法詳解_python
- 2022-03-17 正確使用dotnet-*工具的方法_實用技巧
- 2023-06-19 Docker?查詢、停止、刪除和重啟容器的詳細過程_docker
- 2022-04-20 Python基礎筆記之struct和格式化字符_python
- 2022-03-15 Centos7下NFS服務搭建介紹_Linux
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支