用js實現網頁計算器,JS計算器

本文目錄一覽:

JS實現計算器

用 表單提交 或者超鏈接 或者 AJAX傳遞文本框內輸入的數字

FormBean

int a //輸入數1

int b //輸入數2

String operator// 運算符

Action裡面

先判斷運算符 在調用相應的方法進行處理 返回頁面

如果需要保留 用戶輸入的數字可以用ajax實現 或者Html標籤實現

簡單點就用Ajax實現

如何用JS創建一個簡單的網頁計算器

!doctype html    

html    

head    

title計算器/title    

meta charset=”utf-8″/    

style type=”text/css”    

.panel{    

   border:4px solid #ddd;    

width:192px;    

margin:100px auto;    

}    

.panel p,.panel input{    

   font-family:”微軟雅黑”;    

font-size:20px;    

margin:4px;    

float:left;    

}    

.panel p{    

   width:122px;    

height:26px;    

border:1px solid #ddd;    

padding:6px;    

overflow:hidden;    

}    

.panel input{    

  width:40px;    

height:40px;    

border:1px solid #ddd;    

}    

/style    

script type=”text/javascript”    

//參數e用來接收傳入的event對象    

function cal(e){    

//1.獲取事件源,處理button的事件    

var obj=e.srcElement||e.target;    

if(obj.nodeName !=”INPUT”){    

  return;    

}    

    

var value=obj.value;    

var p=document.getElementById(“screen”);    

if(value==”C”){    

//2.如果是[C],則清空p    

p.innerText=””;    

}else if(value==”=”){    

//3.如果是[=],則運算    

var exp=p.innerText;    

try{    

var result=eval(“(“+exp+”)”);    

//如果正確執行,將結果寫入p    

p.innerText=result;    

}catch(e){    

//發生錯誤,給予錯誤提示    

  p.innerText=”Error.”;    

}    

}else{    

//4.如果是其它按鈕,則將value追加到p中    

p.innerText+=value;    

    

}    

}    

/script    

/head    

body    

!–在最外層的div上註冊單擊事件,傳入event對象,然後在函數中通過event判斷出事件來源於哪一個button,    

    進而做出應有的處理。這樣的好處是,避免在button上大量的註冊事件。–    

div class=”panel” onClick=”cal(event);”    

div    

p id=”screen”/p    

input type=”button” value=”C”    

div style=”clear:both”/div    

/div    

div    

input type=”button” value=”7″    

input type=”button” value=”8″    

input type=”button” value=”9″    

input type=”button” value=”/”    

input type=”button” value=”4″    

input type=”button” value=”5″    

input type=”button” value=”6″    

input type=”button” value=”*”    

input type=”button” value=”1″    

input type=”button” value=”2″    

input type=”button” value=”3″    

input type=”button” value=”-”    

input type=”button” value=”0″    

input type=”button” value=”.”    

input type=”button” value=”=”    

input type=”button” value=”+”    

div style=”clear:both”/div    

/div    

/body    

/html

這是我自學時候寫的計算器

如何用js做一個簡易計算器?

js做一個簡易計算器具體如下:

html

head

titlejs運算/title

boby

table

tr

td第一個數/td

tdinput type=”text” id=”onesum”/td

/tr

tr

td運算符號/td

tdinput type=”text” id=”fh”/td

/tr

tr

td第二個數/td

tdinput type=”text” id=”twosum”/td

/tr

tr

td計算結果/td

tdinput type=”text” id=”sum”/td

/tr

tr

td colspan=”2″input type=”button” value=”   計算   ” onclick=”js()”/td

/tr

table

script

function js(){

var num1=document.getElementById(“onesum”).value;

var num2=document.getElementById(“twosum”).value;

var fh=document.getElementById(“fh”).value;

var sum=0;

nu

m1=Number(num1);

num2=Number(num2);

if(fh==’+’)

{

sum=num1+num2;

}

else if(fh==’-‘)

{

sum=num1-num2;

}else if(fh==’*’)

{

sum=num1*num2;

}else if(fh==’/’)

{

sum=num1/num2;

}

//alert(sum);

document.getElementById(“sum”).value=sum;

}

/script

/boby

/html

JavaScript 教程 JavaScript 是屬於網路的腳本語言! JavaScript 被數百萬計的網頁用來改進設計、驗證表單、檢測瀏覽器、創建cookies,以及更多的應用。

利用JS算術運算符實現一個簡單的頁面計算器功能。效果見下圖:

function check_validate1(value){ //首先只允許那兩個輸入框只能輸入數字

//定義正則表達式部分

var reg = /^\d+$/;

if( value.constructor === String ){

var re = value.match( reg );

return true;

}

return false;

}

function test(id){ //你那些按鈕的ID 你可以自己定一個規則

var num1 = parseFloat($(‘#id1’).val());

var num2 = parseFloat($(‘#id2’).val());

switct(id)

{

case 1 :

return num1 + num2 ;

case 2:

return num1 – num1 ;

case 3

//我這裡的規則就是 1 2 3 4…按照你的按鈕順序來的

}

}

如何使用JS完成一個簡單的計算器功能

你好大意啊,id取錯了

    var firnum = parseInt(document.getElementById(“txt1”).value);   

//獲取第二個輸入框的值

    var secnum = parseInt(document.getElementById(“txt2”).value);

這樣就對了!

如何使用javascript編寫一個計算器

首先,由於JS的存在數值的精度誤差問題:

0.1+0.2   //0.30000000000000004

0.3-0.1   //0.19999999999999998

所以在編寫計算器是應首先解決計算精度問題,以下四個代碼段分別是js中精確的加減乘除運算函數

//浮點數加法運算

function floatAdd(arg1,arg2){

var r1,r2,m;

try{r1=arg1.toString().split(“.”)[1].length}catch(e){r1=0}

try{r2=arg2.toString().split(“.”)[1].length}catch(e){r2=0}

m=Math.pow(10,Math.max(r1,r2));

return (arg1*m+arg2*m)/m

}

//浮點數減法運算

function floatSub(arg1,arg2){

   var r1,r2,m,n;

   try{r1=arg1.toString().split(“.”)[1].length}catch(e){r1=0}

   try{r2=arg2.toString().split(“.”)[1].length}catch(e){r2=0}

   m=Math.pow(10,Math.max(r1,r2));

   //動態控制精度長度

   n=(r1=r2)?r1:r2;

   return ((arg1*m-arg2*m)/m).toFixed(n);

}

//浮點數乘法運算

function floatMul(arg1,arg2){

   var m=0,s1=arg1.toString(),s2=arg2.toString();

   try{m+=s1.split(“.”)[1].length}catch(e){}

   try{m+=s2.split(“.”)[1].length}catch(e){}

   return Number(s1.replace(“.”,””))*Number(s2.replace(“.”,””))/Math.pow(10,m)

}

//浮點數除法運算

function floatDiv(arg1,arg2) {

   var t1 = 0, t2 = 0, r1, r2;

   try {t1 = arg1.toString().split(“.”)[1].length} catch (e) {}

   try {t2 = arg2.toString().split(“.”)[1].length} catch (e) {}

   with (Math) {

       r1 = Number(arg1.toString().replace(“.”, “”));

       r2 = Number(arg2.toString().replace(“.”, “”));

       return (r1 / r2) * pow(10, t2 – t1);

   }

}

以下是詳細的計算器代碼: 

HTML5

!DOCTYPE html

html lang=”en”

head

meta charset=”UTF-8″

title簡單計算器/title

link href=”main.css” rel=”stylesheet”

/head

body

div id=”calculator”

div id=”calculator_container”

h3計算器/h3

table id=”calculator_table”

tbody

tr

td colspan=”5″

input type=”text” id=”resultIpt” readonly=”readonly” value=”” size=”17″ maxlength=”17″ style=”width:294px;color: black”

/td

/tr

tr

tdinput type=”button” value=”←”       class=”btn_color1 btn_operation”/td

tdinput type=”button” value=”全清”     class=”btn_color1 btn_operation”/td

tdinput type=”button” value=”清屏”     class=”btn_color1″/td

tdinput type=”button” value=”﹢/﹣”    class=”btn_color2 btn_operation”/td

tdinput type=”button” value=”1/×”     class=”btn_color2 btn_operation”/td

/tr

tr

tdinput type=”button”  value=”7″     class=”btn_color3 btn_number”/td

tdinput type=”button”  value=”8″     class=”btn_color3 btn_number”/td

tdinput type=”button”  value=”9″     class=”btn_color3 btn_number”/td

tdinput type=”button”  value=”÷”    class=”btn_color4 btn_operation”/td

tdinput type=”button”  value=”%”    class=”btn_color2 btn_operation”/td

/tr

tr

tdinput type=”button”   value=”4″   class=”btn_color3 btn_number”/td

tdinput type=”button”   value=”5″   class=”btn_color3 btn_number”/td

tdinput type=”button”   value=”6″   class=”btn_color3 btn_number”/td

tdinput type=”button”   value=”×”  class=”btn_color4 btn_operation”/td

tdinput type=”button”   value=”√”  class=”btn_color2 btn_operation”/td

/tr

tr

tdinput type=”button”  value=”1″   class=”btn_color3 btn_number”/td

tdinput type=”button”  value=”2″   class=”btn_color3 btn_number”/td

tdinput type=”button”  value=”3″   class=”btn_color3 btn_number”/td

tdinput type=”button”  value=”-”  class=”btn_color4 btn_operation”/td

td rowspan=”2″

input type=”button”  value=”=”  class=”btn_color2″ style=”height: 82px” id=”simpleEqu”

/td

/tr

tr

td colspan=”2″

input type=”button”  value=”0″   class=”btn_color3 btn_number” style=”width:112px”

/td

tdinput type=”button”  value=”.”   class=”btn_color3 btn_number” /td

tdinput type=”button”  value=”+”  class=”btn_color4 btn_operation”/td

/tr

/tbody

/table

/div

/div

script type=”text/javascript” src=”calculator.js”/script

/body

/html

CSS3

* {

margin: 0;

padding: 0;

}

#calculator{

position: relative;

margin: 50px auto;

width: 350px;

height: 400px;

border: 1px solid gray;

-webkit-border-radius: 10px;

-moz-border-radius: 10px;

border-radius: 10px;

-webkit-box-shadow: 2px 2px 4px gray;

-moz-box-shadow: 2px 2px 4px gray;

box-shadow: 2px 2px 4px gray;

behavior:url(“ie-css3.htc”);  /*IE8-*/

}

#calculator_table{

position: relative;

margin: 10px auto;

border-collapse:separate;

border-spacing:10px 20px;

}

h3{

position: relative;

width: 60px;

height: 30px;

margin: 0 auto;

}

#calculator_table td{

width: 50px;

height: 30px;

border: 1px solid gray;

-webkit-border-radius: 2px;

-moz-border-radius: 2px;

border-radius: 2px;

behavior:url(“ie-css3.htc”);  /*IE8-*/

}

#calculator_table td input{

font-size: 16px;

border: none;

width: 50px;

height: 30px;

color: white;

}

input.btn_color1{

background-color: orange;

}

input.btn_color2{

background-color: #133645;

}

input.btn_color3{

background-color: #59807d;

}

input.btn_color4{

background-color: seagreen;

}

input:active{

-webkit-box-shadow: 3px 3px 3px gray;

-moz-box-shadow: 3px 3px 3px gray;

box-shadow: 3px 3px 3px gray;

behavior:url(“ie-css3.htc”);  /*IE8-*/

}

JS

window.onload=function() {

var resultIpt = document.getElementById(“resultIpt”); //獲取輸出文本框

var btns_number = document.getElementsByClassName(“btn_number”); //獲取數字輸入按鈕

var btns_operation = document.getElementsByClassName(“btn_operation”); //獲取操作按鈕

var simpleEqu = document.getElementById(“simpleEqu”); //獲取”=”按鈕

var temp = “”;

var num1= 0,num2=0;

//獲取第一個數

for(var i=0;ibtns_number.length;i++){

btns_number[i].onclick=function (){

temp += this.value;

resultIpt.value = temp;

};

}

//對獲取到的數進行操作

for(var j=0;jbtns_operation.length;j++) {

btns_operation[j].onclick = function () {

num1=parseFloat(resultIpt.value);

oper = this.value;

if(oper==”1/×”){

num1 = Math.pow(num1,-1); //取倒數

resultIpt.value = num1.toString();

}else if(oper==”﹢/﹣”){    //取相反數

num1 = -num1;

resultIpt.value = num1.toString();

}else if(oper==”√”){      //取平方根

num1 =Math.sqrt(num1);

resultIpt.value = num1.toString();

}else if(oper==”←”){    //刪除個位

resultIpt.value = resultIpt.value.substring(0, resultIpt.value.length – 1);

}else if(oper==”全清”){  //清除數字

resultIpt.value = “”;

}

else{          //oper==”+” “-” “×” “÷” “%”時,繼續輸入第二數字

temp = “”;

resultIpt.value = temp;

}

}

}

//輸出結果

simpleEqu.onclick=function(){

num2=parseFloat(temp);  //取得第二個數字

calculate(num1, num2, oper);

resultIpt.value = result.toString();

}

};

//定義一個計算函數

function calculate(num1, num2, oper) {

switch (oper) {

case “+”:

result=floatAdd(num1, num2); //求和

break;

case “-”:

result=floatSub(num1, num2); //求差

break;

case “×”:

result=floatMul(num1, num2);  //求積

break;

case “÷”:

result=floatDiv(num1, num2);  //求商

break;

case “%”:

result=num1%num2;  //求餘數

break;

}

}

//精確計算

//浮點數加法運算

function floatAdd(arg1,arg2){

var r1,r2,m;

try{r1=arg1.toString().split(“.”)[1].length}catch(e){r1=0}

try{r2=arg2.toString().split(“.”)[1].length}catch(e){r2=0}

m=Math.pow(10,Math.max(r1,r2));

return (arg1*m+arg2*m)/m

}

//浮點數減法運算

function floatSub(arg1,arg2){

var r1,r2,m,n;

try{r1=arg1.toString().split(“.”)[1].length}catch(e){r1=0}

try{r2=arg2.toString().split(“.”)[1].length}catch(e){r2=0}

m=Math.pow(10,Math.max(r1,r2));

//動態控制精度長度

n=(r1=r2)?r1:r2;

return ((arg1*m-arg2*m)/m).toFixed(n);

}

//浮點數乘法運算

function floatMul(arg1,arg2){

var m=0,s1=arg1.toString(),s2=arg2.toString();

try{m+=s1.split(“.”)[1].length}catch(e){}

try{m+=s2.split(“.”)[1].length}catch(e){}

return Number(s1.replace(“.”,””))*Number(s2.replace(“.”,””))/Math.pow(10,m)

}

//浮點數除法運算

function floatDiv(arg1,arg2) {

var t1 = 0, t2 = 0, r1, r2;

try {t1 = arg1.toString().split(“.”)[1].length} catch (e) {}

try {t2 = arg2.toString().split(“.”)[1].length} catch (e) {}

with (Math) {

r1 = Number(arg1.toString().replace(“.”, “”));

r2 = Number(arg2.toString().replace(“.”, “”));

return (r1 / r2) * pow(10, t2 – t1);

}

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/238894.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:13
下一篇 2024-12-12 12:13

相關推薦

  • JS Proxy(array)用法介紹

    JS Proxy(array)可以說是ES6中非常重要的一個特性,它可以代理一個數組,監聽數據變化並進行攔截、處理。在實際開發中,使用Proxy(array)可以方便地實現數據的監…

    編程 2025-04-29
  • 解析js base64並轉成unit

    本文將從多個方面詳細介紹js中如何解析base64編碼並轉成unit格式。 一、base64編碼解析 在JavaScript中解析base64編碼可以使用atob()函數,它會將b…

    編程 2025-04-29
  • Node.js使用Body-Parser處理HTTP POST請求時,特殊字元無法返回的解決方法

    本文將解決Node.js使用Body-Parser處理HTTP POST請求時,特殊字元無法返回的問題。同時,給出一些相關示例代碼,以幫助讀者更好的理解並處理這個問題。 一、問題解…

    編程 2025-04-29
  • python爬取網頁並生成表格

    本文將從以下幾個方面詳細介紹如何使用Python爬取網頁數據並生成表格: 一、獲取網頁數據 獲取網頁數據的一般思路是通過HTTP請求獲取網頁內容,最常用的方式是使用Python庫r…

    編程 2025-04-28
  • 網頁防篡改的重要性和市場佔有率

    網頁防篡改對於保護網站安全和用戶利益至關重要,而市場上針對網頁防篡改的產品和服務也呈現出不斷增長的趨勢。 一、市場佔有率 據不完全統計,目前全球各類網頁防篡改產品和服務的市場規模已…

    編程 2025-04-28
  • t3.js:一個全能的JavaScript動態文本替換工具

    t3.js是一個非常流行的JavaScript動態文本替換工具,它是一個輕量級庫,能夠很容易地實現文本內容的遞增、遞減、替換、切換以及其他各種操作。在本文中,我們將從多個方面探討t…

    編程 2025-04-28
  • Python編程實戰:用Python做網頁與HTML

    Python語言是一種被廣泛應用的高級編程語言,也是一種非常適合於開發網頁和處理HTML的語言。在本文中,我們將從多個方面介紹如何用Python來編寫網頁和處理HTML。 一、Py…

    編程 2025-04-28
  • Python爬取網頁信息

    本文將從多個方面對Python爬取網頁信息做詳細的闡述。 一、爬蟲介紹 爬蟲是一種自動化程序,可以模擬人對網頁進行訪問獲取信息的行為。通過編寫代碼,我們可以指定要獲取的信息,將其從…

    編程 2025-04-28
  • JS圖片沿著SVG路徑移動實現方法

    本文將為大家詳細介紹如何使用JS實現圖片沿著SVG路徑移動的效果,包括路徑製作、路徑效果、以及實現代碼等內容。 一、路徑製作 路徑的製作,我們需要使用到SVG,SVG是可縮放矢量圖…

    編程 2025-04-27
  • 使用Python轉髮網頁內容

    Python是一種廣泛使用的編程語言,它在網路爬蟲、數據分析、人工智慧等領域都有廣泛的應用。其中,使用Python轉髮網頁內容也是一個常見的應用場景。在本文中,我們將從多個方面詳細…

    編程 2025-04-27

發表回復

登錄後才能評論