用JavaScript做一个简易的计算器

用JavaScript做一个简易的计算器,第1张

我刚学javaScript做一计算器,请看看!多多交流!~~``

<html>

<head>

<title>计算器</title>

</head>

<body bgcolor="#33FF99" onload="FKeyPadReadOutfocus();FKeyPadReadOutselect();">

<FORM name="Keypad" action="">

<TABLE align="center">

<B>

<TABLE align="center" border=3 width=60 height=80 cellpadding=2 cellspacing=5 borderlight="#008080">

<tr align="center">

<td bordercolor="#FF33CC" bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933" colspan=10 align=center>网页计算器</td>

</tr>

<tr align="center">

<td bordercolor="#FF33CC" bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933" colspan=10 align=right><input name="ReadOut" type="Text" onkeypress="CheckOut()" size=40 value="0"

width=100%></td>

</tr>

<tr align="center">

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnsin" type="Button" value=" sin " onclick="useFun ('sin')"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btncos" type="Button" value=" cos " onclick="useFun ('cos')"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnOne" type="Button" value=" 1 " onclick="NumPressed(1)"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnTwo" type="Button" value=" 2 " onclick="NumPressed(2)"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnThree" type="Button" value=" 3 " onclick="NumPressed(3)"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnPlus" type="Button" value=" + " onclick="Operation('+')"> </td>

</tr>

<tr align="center">

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnasin" type="Button" value=" asin " onclick="useFun ('asin')"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnacos" type="Button" value=" acos " onclick="useFun ('acos')"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnFour" type="Button" value=" 4 " onclick="NumPressed(4)"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnFive" type="Button" value=" 5 " onclick="NumPressed(5)"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnSix" type="Button" value=" 6 " onclick="NumPressed(6)"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnMinus" type="Button" value=" - " onclick="Operation('-')"></td>

</tr>

<tr align="center">

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btntan" type="Button" value=" tan " onclick="useFun ('tan')"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnatan" type="Button" value=" atan " onclick="useFun ('atan')"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnSeven" type="Button" value=" 7 " onclick="NumPressed(7)"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnEight" type="Button" value=" 8 " onclick="NumPressed(8)"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnNine" type="Button" value=" 9 " onclick="NumPressed(9)"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnMultiply" type="Button" value=" " onclick="Operation('')"></td>

</tr>

<tr align="center">

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnlog" type="Button" value=" log " onclick="useFun ('log')"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnexp" type="Button" value=" exp " onclick="useFun ('exp')"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnZero" type="Button" value=" 0 " onclick="NumPressed(0)"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnDecimal" type="Button" value=" " onclick="Decimal()"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnNeg" type="Button" value=" +/- " onclick="Neg()"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnDivide" type="Button" value=" / " onclick="Operation('/')"></td>

</tr>

<tr align="center">

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnsqrt" type="Button" value=" sqrt " onclick="useFun ('sqrt')"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnOx" type="Button" value=" 1/x " onclick="useFun ('1/x')"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnXt" type="Button" value=" x^2 " onclick="useFun ('x^2')"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnXthr" type="Button" value=" x^3 " onclick="useFun ('x^3')"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnpi" type="Button" value=" pi " onclick=FKeyPadReadOutvalue=MathPI></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnEquals" type="Button" value=" = " onclick="Operation('=')"></td>

</tr>

<tr align="center">

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnDivide" type="Button" value=" N! " onclick="ni ()"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnDivide" type="Button" value=" pow " onclick="Operation('pow')"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnDivide" type="Button" value=" E " onclick=FKeyPadReadOutvalue=MathE></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnClear" type="Button" value=" C " onclick="Clear()"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnClearEntry" type="Button" value=" CE " onclick="ClearEntry()"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnReturn" type="Button" value="返 回" onclick="goReturn()"></td>

</tr>

<tr align="center">

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnmod" type="Button" value=" MOD " onclick="Operation('MOD')"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnDivide" type="Button" value=" INT" onclick=FKeyPadReadOutvalue=parseInt(FKeyPadReadOutvalue,10)></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnmod" type="Button" value=" HEX " onclick="hexchange ()"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnmod" type="Button" value=" OCT " onclick="octchange ()"></td>

<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnmod" type="Button" value=" BIN " onclick="binchange ()"></td>

</tr>

</TABLE>

</TABLE>

</B>

</FORM>

</CENTER>

<font face="Verdana, Arial, Helvetica" size=2>

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin

var FKeyPad = documentforms['Keypad'];

var Accum = "0";

var FlagNewNum = false;

var PendingOp = "";

function longCount(str1,str2,type)

{

var comma1 = 0;

if (str1indexOf("")!=-1)

{

str1 = str1replace(/0$/,"");

comma1 = str1length - str1indexOf("")-1;

}

var comma2 = 0;

if (str2indexOf("")!=-1)

{

str2 = str2replace(/0$/,"");

comma2 = str2length - str2indexOf("")-1;

}

str1 = str1replace(/\/,"");

str2 = str2replace(/\/,"");

var value,comma;

if (type!="")

{

if (comma1>comma2)

{

for (var i=0;i<comma1-comma2;i++) str2 += "0";

comma = (type=="/")0:comma1;

}else

{

for (var i=0;i<comma2-comma1;i++) str1 += "0";

comma = (type=="/")0:comma2;

}

}else

{

comma = comma1 + comma2;

}

if (type=="+")

{

value = parseInt(str1,10) + parseInt(str2,10);

}else if (type=="-")

{

value = parseInt(str1,10) - parseInt(str2,10);

}else if (type=="")

{

value = parseInt(str1,10) parseInt(str2,10);

}else if (type=="/")

{

value = parseInt(str1,10) / parseInt(str2,10);

}

else if (type=="MOD")

value = parseInt(str1,10) % parseInt(str2,10);

else if (type=="pow")

{

var a,b,s=1;

a=parseInt(str1,10);

b=parseInt(str2,10);

for(var i=1;i<=b;i++)

s=sa;

value=parseInt(s,10);

}

value = String(value);

if (comma>0) value = valuesubstring(0,valuelength-comma)+""+valuesubstring(valuelength- comma,valuelength);

if (valueindexOf("")!=-1)

value = valuereplace(/0$/,"");

return value;

}

function NumPressed (Num) {

if (FlagNewNum)

{

FKeyPadReadOutvalue = Num;

FlagNewNum = false;

}

else

{

if (FKeyPadReadOutvalue == "0")

FKeyPadReadOutvalue = Num;

else

FKeyPadReadOutvalue += Num;

}

}

function Operation (Op)

{

var Readout = FKeyPadReadOutvalue;

if (FlagNewNum && PendingOp != "=");

else

{

FlagNewNum = true;

if ( '+' == PendingOp || '-' == PendingOp || '/' == PendingOp || '' == PendingOp||'MOD'==PendingOp||'pow'==PendingOp)

Accum = longCount(Accum,Readout,PendingOp);

else

Accum = Readout;

FKeyPadReadOutvalue = Accum;

PendingOp = Op;

FKeyPadReadOutfocus();

FKeyPadReadOutselect();

}

}

function useFun (st)

{

switch(st)

{

case "sin":

FKeyPadReadOutvalue=Mathsin(FKeyPadReadOutvalue);

FlagNewNum=true;

break;

case "cos":

FKeyPadReadOutvalue=Mathcos(FKeyPadReadOutvalue);

FlagNewNum=true;

break;

case "asin":

FKeyPadReadOutvalue=Mathasin(FKeyPadReadOutvalue);

FlagNewNum=true;

break;

case "acos":

FKeyPadReadOutvalue=Mathacos(FKeyPadReadOutvalue);

FlagNewNum=true;

break;

case "tan":

FKeyPadReadOutvalue=Mathtan(FKeyPadReadOutvalue);

FlagNewNum=true;

break;

case "atan":

FKeyPadReadOutvalue=Mathatan(FKeyPadReadOutvalue);

FlagNewNum=true;

break;

case "log":

FKeyPadReadOutvalue=Mathlog(FKeyPadReadOutvalue);

FlagNewNum=true;

break;

case "exp":

FKeyPadReadOutvalue=Mathexp(FKeyPadReadOutvalue);

FlagNewNum=true;

break;

case "sqrt":

FKeyPadReadOutvalue=Mathsqrt(FKeyPadReadOutvalue);

FlagNewNum=true;

break;

case "1/x":

FKeyPadReadOutvalue=1/FKeyPadReadOutvalue;

FlagNewNum=true;

break;

case "x^2":

FKeyPadReadOutvalue=parseFloat(FKeyPadReadOutvalue)parseFloat(FKeyPadReadOutvalue);

FlagNewNum=true;

break;

case "x^3":

FKeyPadReadOutvalue=parseFloat(FKeyPadReadOutvalue)parseFloat(FKeyPadReadOutvalue)parseFloat(FKeyPadReadOutvalue);

FlagNewNum=true;

break;

}

}

function hexchange ()

{

var hex,dec;

dec = parseFloat(FKeyPadReadOutvalue);

hex = dectoString(16);

windowalert("该十进制数转成十六进制数为:"+ hex);

}

function octchange ()

{

var oct,dec;

dec = parseFloat(FKeyPadReadOutvalue);

oct = dectoString(8);

windowalert("该十进制数转成八进制数为:"+ oct);

}

function binchange ()

{

var bin,dec;

dec = parseFloat(FKeyPadReadOutvalue);

bin = dectoString(2);

windowalert("该十进制数转成二进制数为:"+ bin);

}

function ni ()

{var y,k=1;

y=parseFloat(FKeyPadReadOutvalue);

for(var x=1;x<=y;x++)

k=kx;

FKeyPadReadOutvalue=k;

}

function Decimal ()

{

var curReadOut = FKeyPadReadOutvalue;

if (FlagNewNum)

{

curReadOut = "0";

FlagNewNum = false;

}

else

{

if (curReadOutindexOf("") == -1)

curReadOut += "";

}

FKeyPadReadOutvalue = curReadOut;

}

function ClearEntry ()

{

FKeyPadReadOutvalue = "0";

FlagNewNum = true;

}

function Clear ()

{

Accum = "0";

PendingOp = "";

ClearEntry();

}

function Neg ()

{

windowalert(FKeyPadReadOutvalue);

FKeyPadReadOutvalue = longCount(FKeyPadReadOutvalue,"-1","");

}

function goReturn()

{

topreturnValue = FKeyPadReadOutvalue;

selfclose();

}

function CheckOut()

{

var keyCode = windoweventkeyCode;

if (keyCode>=48 && keyCode<=57)

{

if (FlagNewNum)

{

FKeyPadReadOutvalue = "";

FlagNewNum = false;

}

return true;

}else if (keyCode==43 || keyCode==45 || keyCode==42 || keyCode==47 || keyCode==61) {

Operation(StringfromCharCode(keyCode));

}else if (keyCode==46) {

if (FKeyPadReadOutvalueindexOf("") == -1)

return true;

}else if (keyCode==27) goReturn();

windoweventreturnValue = false;

return false;

}

// End -->

</SCRIPT>

</body>

</html>

<!DOCTYPE html>

<html>

<head>

<title>cc-vuejs</title>

<style type="text/css">

</style>

<script>

// 生成2019年元旦日期函数

function _yd() {

var yd = new Date();

// 2019 - 01-01

// Year

ydsetYear(ydgetFullYear() + 1);

// Month

ydsetMonth(0);

// Day

ydsetDate(1);

// Time

ydsetHours(0,0,0,0);

return yd;

}

/

   计算当前时间距元旦时间间隔

 /

function yd_cal() {

var ipt = documentgetElementById('ipt');

var now = new Date();

var ts = _yd()getTime() - nowgetTime();

var _d = ts / (24  60  60  1000);

// consolelog('天:' + parseInt(_d));

var _h = (_d - parseInt(_d))  24;

// consolelog('小时:' + parseInt(_h));

var _m = (_h - parseInt(_h))  60;

// consolelog("分钟:" + parseInt(_m));;

var _s = (_m - parseInt(_m))  60;

// consolelog("秒:" + parseInt(_s));;

iptvalue = " " + parseInt(_d)  + "天 " +  parseInt(_h) + ":" + parseInt(_m) + ":" + parseInt(_s) + "" 

}

</script>

</head>

<body style="margin: 20px;">

<div style="width: 100%; height: 40px; background-color: #fff">

<label for="ipt" style="font-size: 8em">当前距元旦时间间隔:</label>

<input type="text" value="" placeholder="元旦时间间隔" disabled id="ipt">

<input type="button" value="计算" onclick="yd_cal()" />

</div>

</body>

</html>

%在js中是取余的意思。

循环i % 4,当i为0时结果为0,setColor[0]得到"red"

循环i % 4,当i为1时结果为1,setColor[1]得到"yellow"

循环i % 4,当i为6时结果为2,setColor[2]得到"blue"

直到全部循环完成。

是正确的啊

是想应用在多个表吗?

如果是的,就:

<script type="text/javascript">

function bgShow(who){

// var item = documentgetElementById('tb'); //获取id为tb的元素(table)

var item=who;

var tbody = itemgetElementsByTagName("tbody")[0]; //获取表格的第一个tbody元素

var trs = tbodygetElementsByTagName("tr"); //获取tbody元素下的所有tr元素

for(var i=0;i < trslength;i++){//循环tr元素

if(i%2==0){ //取模 (取余数比如 0%2=0 , 1%2=1 , 2%2=0 , 3%2=1)

trs[i]stylebackgroundColor = "#E7E7E7"; // 改变 符合条件的tr元素 的背景色

}

}

}

windowonload = function(){

var tables=documentgetElementsByTagName('table');

for(var n=0,m=tableslength;n<m;n++)

bgShow(tables[n]);

}

</script>

以上就是关于用JavaScript做一个简易的计算器全部的内容,包括:用JavaScript做一个简易的计算器、js编程编一个程序,求当前时间距离元旦的剩余时间,在input文本框中显示出来。、关于javascript中百分号的用法等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9313587.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存