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

计算器中关于perseInt ?

5940095a0001198006580493.jpg

a=parseInt(a,10);
这样表达就会出错。
而在switch里,anser=parseInt(a,10)+parseInt(b,10);这样就正确了呢?
我查了一下parseInt,它会返回一个字符串的整数,如果没有数字,返回NaN,这个返回值为啥不能再赋值给a呢?

正在回答

1 回答

 function count(){
    var txt1 = document.getElementById('txt1').value;
    txt1 = parseInt(txt1,10);                           //txt1不用加引号
    var txt2 = document.getElementById('txt2').value;
    txt2 = parseInt(txt2,10);                           //txt2不用加引号
    var select = document.getElementById('select').value;
    var txt;
    switch(select){
        case '+':
            txt = txt1 + txt2;
            break;
        case '-':
            txt = txt1 - txt2;
            break;
        case '*':
            txt = txt1 * txt2;
            break;
        case '/':
            txt = txt1 / txt2;
            break;
        default:
            "-.-";
    }
    
    document.getElementById('fruit').value = txt;
   }


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
JavaScript进阶篇
  • 参与学习       467117    人
  • 解答问题       21874    个

本课程从如何插入JS代码开始,带您进入网页动态交互世界

进入课程

计算器中关于perseInt ?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信