zoukankan      html  css  js  c++  java
  • JavaScript求和

    
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>求和</title>
        <script>
            window.onload=function(){
                var oTxt1=document.getElementById('txt1');
                var oTxt2=document.getElementById('txt2');
                var oBtn=document.getElementById('btn1');
                oBtn.onclick=function(){
                    alert(parseInt(oTxt1.value)+parseInt(oTxt2.value));
                }
            }
        </script>
    </head>
    <body>
    <input id="txt1" type="text"/>
    <input id="txt2" type="text"/>
    <input id="btn1" type="button" value="求和">
    
    </body>
    </html>

     

    NaN:not a number不是数字

    12+NaN=NaN

    NaN!=NaN

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>NaN</title>
    </head>
    <script>
        var a=parseInt('abc');
        var b=parseInt('def');
        alert(a==b);
    </script>
    <body>
    </body>
    </html>

     判断是否是NaN

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>nan</title>
    <script>
        var a=parseInt('abc');
        alert(isNaN(a));
    </script>
    </head>
    <body>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>求和</title>
        <script>
            window.onload=function() {
                var oTxt1 = document.getElementById('txt1');
                var oTxt2 = document.getElementById('txt2');
                var oBtn= document.getElementById('btn1');
    
                oBtn.onclick=function(){
                    var n1=parseInt(oTxt1.value);
                    var n2=parseInt(oTxt2.value);
                    if(isNaN(n1)){
                        alert('您输入的第一个数字有误');}
    
                    else if(isNaN(n2)){
                            alert('您输入的第二个数字有误');
                        }
                    else{
                    alert(n1+n2);
                }
            }
            }
        </script>
    </head>
    <body>
    <input id="txt1" type="text"/>
    <input id="txt2" type="text"/>
    <input id="btn1" type="button" value="求和">
    
    </body>
    </html>

    FLOAT

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <script>
        var a='3.5';
        //alert(parseInt(a));
        alert(parseFloat(a));
    </script>
    </body>
    </html>

  • 相关阅读:
    Rational工具介绍(转)
    MySQL表的存储引擎
    2009 年度最佳 jQuery 插件
    09年关门歇业的15大网站 雅虎旗下4网站上榜
    [转载]windows2003上IIS+PyISAPIe1.1..0部署成功
    安装IronPythonStudio出错:已解决
    C# 开源项目
    Cassandra在windows平台下安装布署,对No SQL感兴趣的朋友可以进来看看
    【转载】开发人员用的一些好网站
    [转载]用来武装Firebug的十三款Firefox插件
  • 原文地址:https://www.cnblogs.com/Yimi/p/6003807.html
Copyright © 2011-2022 走看看