本文目錄一覽:
簡易的加減乘除的計算器代碼js
//html
input type=”text” id=”num1″ value=”” /
select id=”mySelect”
option value=”+”+/option
option value=”-“-/option
option value=”*”*/option
option value=”/”//option
/select
input type=”text” id=”num2″ value=”” /
input type=”button” id=”jisuan” value=”計算” /
//js
script
var oTxt1 = document.getElementById(‘num1’);
var oTxt2 = document.getElementById(‘num2’);
var oSelect = document.getElementById(‘mySelect’);
var oBtn = document.getElementById(‘jisuan’);
oBtn.onclick=function(){
switch(oSelect.value){
case ‘+’:
alert(parseInt(oTxt1.value)+parseInt(oTxt2.value));
break;
case ‘-‘:
alert(parseInt(oTxt1.value)-parseInt(oTxt2.value));
break;
case ‘*’:
alert(parseInt(oTxt1.value)*parseInt(oTxt2.value));
break;
case ‘/’:
if(parseInt(oTxt2.value) !== 0){
alert(parseInt(oTxt1.value)/parseInt(oTxt2.value));
}else{
alert(‘除數不能為0’);
}
break;
default:
alert(‘Bug!!!’);
}
}
/script
如何用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 test(){
var txt1 = document.getElementById(“txt1”),
txt2 = document.getElementById(“txt2”),
txt3 = document.getElementById(“txt3”),
opt = document.getElementById(“sel”);
txt3.value = eval(txt1.value + opt.value + txt2.value);//eval函數可計算某個字元串,並執行其中的的js代碼
}
input type=”text” id=”txt1″ /
select id=”sel”
option value=”+”+/option
option value=”-“-/option
option value=”*”*/option
option value=”/”//option
/select
input type=”text” id=”txt2″ /
=
input type=”text” id=”txt3″ /
input type=”button” id=”btn” value=”計算” onclick=”test()”/
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/258663.html