为了账号安全,请及时绑定邮箱和手机立即绑定

想问问简单的计算器中JS代码是什么意思?

想问问简单的计算器中JS代码是什么意思?

慕虎9706840 2017-03-15 21:12:04
<!doctype html><html><head><meta charset="utf-8"><script type="text/javascript" src="counter.js"></script></head><body>   <input type='text' id='txt1' style="width:30px"/>    <select id='select'> <option value='+'>+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option>   </select>   <input type='text' id='txt2' style="width:30px" />    <input type='button' value=' = '  onclick="count()"/> <!--通过 = 按钮来调用创建的函数,得到结果-->    <input type='text' id='fruit' />  </body></html>function count(){             //获取第一个输入框的值     var vtxt1=document.getElementById("txt1").value; //获取第二个输入框的值 var vtxt2=document.getElementById("txt2").value; //获取选择框的值 var symbol=document.getElementById("select").value; //获取通过下拉框来选择的值来改变加减乘除的运算法则 var result; switch(symbol){ case "+": result=parseInt(vtxt1)+parseInt(vtxt2); break; case "-": result=parseInt(vtxt1)-parseInt(vtxt2); break; case "*": result=parseInt(vtxt1)*parseInt(vtxt2); break; default: result=parseInt(vtxt1)/parseInt(vtxt2); }     //设置结果输入框的值      document.getElementById("fruit").value=result;    }为什么document.getElementById("txt1").value;后面加.value这个字,有什么用啊?为什么要声明result呢?为什么不可以直接用symbol,比如:symbol=vtxt1+vtxt2;括号里什么时候加引号啊,我看getElementById("txt1")加了引号呢,而后面parseInt(vtxt1)没有加引号;同理啊,document.getElementById("fruit").value=result;这里啊,这个value有啥用啊?整句话有啥用啊?case "+":这里为啥要加引号呢?parseInt()有啥用呢?为啥输出不了输出小数?我看有些代码写var vtxt2=parseInt(document.getElementById("txt2").value);有些写在result=parseInt(vtxt1)/parseInt(vtxt2);这个里面,有啥区别?最后一个弱弱的问题,var的作用域的问题,这个我搞不懂?
查看完整描述

1 回答

  • 1 回答
  • 1 关注
  • 1204 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信