爱问知识人 爱问教育 医院库

实用计算器JS代码怎么写呢?

首页

实用计算器JS代码怎么写呢?

实用计算器JS代码怎么写呢?

提交回答

全部答案

    2018-10-27 23:14:12
  •   html xmlns=http://www。w3。org/1999/xhtml head title标题页-学无忧(www。xue51。com)/titlescript language=JavaScriptvar results='' ; //显示结果var previouskey=''; //代表上一个字符var re=/(\/|\*|\+|-)/ //用来判断+-*/的正则var re2=/(\/|\*|\+|-){2}$/; //用来判断出现2次+-*/的正则var re3=/。
      +(\/|\*|\+|-)。+/; //用来判断小数点+-*/的正则var re4=/\d|\。/ ; //用来判断(小数点或数值)的正则var re5=/^[^\/\*\+]。+\d$/; //用来判断(以+-*/开始)的正则var re6=/\。
      /; //用来判断小数点的正则//计算结果的方法function calculate(){//判断如何用户输入了1个值,然后单击了=if (event。srcElement。tagName==TD){if (event。srcElement。
      innerText。match(re4)previouskey===)results='';if (result。innerText。match(re3)event。srcElement。innerText。match(re)){if (!results。
      match(re5)){result。innerText=输入错误!;return;}//以下是要求长度只能保持在12位以内(有小数点的情况下)results=eval(results); //转换为数值if (results。toString()。
      length=12results。toString()。match(re6))results=results。toString()。substring(0,12);result。innerText=results;}//出现2次计算符号的情形results+=event。
      srcElement。innerText;if (results。match(re2))results=results。substring(0,results。length-2)+results。charAt(results。length-1);result。
      innerText=results;}}function calculateresult(){//当首字符输入错误时if (!results。match(re5)){result。innerText=输入错误!;return;}results=eval(results); //转换结果为数值型if (results。
      toString()。length=12results。toString()。match(re6))results=results。toString()。substring(0,12);result。innerText=results;}function pn(){//首字符为负数的计算if (result。
      innerText。charAt(0)!='-')result。innerText=results='-'+result。innerTextelse if (result。innerText。charAt(0)=='-')result。innerText=results=result。
      innerText*(-1)}/script需要在body中添加一个Table,用来呈现计算器的外观,代码如下所示:/headbodytable border=0 cellspacing=0 cellpadding=0 width=400trtd width=100% valign=toptable border=2 width=200 cellspacing=0cellpadding=0 bgcolor=#000000 style=border-color:blackonClick=previouskey=event。
      srcElement。innerTexttrtd width=100% bgcolor=#FFFFFF id=resultstyle=font:bold 20px Verdana;color:black;text-align='right'0/td/trtrtd width=100% valign=middle align=centertable border=0 width=100%cellspacing=0 cellpadding=0 style=font:bold 20px Verdana;color:whitetrtd width=80% align=centertable border=1 width=100% cellspacing=0cellpadding=0 style=cursor:hand;font:bold 20px Verdana;color:whiteonMouseover=if (event。
      srcElement。tagName=='TD')event。srcElement。style。color='yellow'onMouseout=event。srcElement。style。color='white' onselectStart=return falseonClick=calculate() height=82trtd width=25% align=center height=177/tdtd width=25% align=center height=178/tdtd width=25% align=center height=179/tdtd width=25% align=center height=17//td/trtrtd width=25% align=center height=194/tdtd width=25% align=center height=195/tdtd width=25% align=center height=196/tdtd width=25% align=center height=19*/td/trtrtd width=25% align=center height=191/tdtd width=25% align=center height=192/tdtd width=25% align=center height=193/tdtd width=25% align=center height=19-/td/trtrtd width=25% align=center height=190/tdtd width=25% align=center height=19onClick=pn();previouskey=1;event。
      cancelBubble=true+/-/tdtd width=25% align=center height=19。/tdtd width=25% align=center height=19+/td/tr/table/tdtd width=20%div align=lefttable border=1 width=100% cellspacing=0cellpadding=0trtd width=100% style=cursor:hand;font:bold 20px Verdana;color:white;text-align:centeronClick=result。
      innerText=0;results=''C/td/tr/table/divdiv align=lefttable border=1 width=100% cellspacing=0 cellpadding=0height=81trtd width=100% style=cursor:hand;font:bold 32px Verdana;color:white;text-align:centeronMouseover=event。
      srcElement。style。color='yellow'onMouseout=event。srcElement。style。color='white' onClick=calculateresult()=/body/html。

    x***

    2018-10-27 23:14:12

类似问题

换一换

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):