• 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>

  • 相关阅读:
    Oneops运维系统
    采集日志工具fluentbit输出到kafka
    logstash
    Redux学习笔记
    部分浏览器不支持 replaceAll的解决
    windows10系统文件浏览器不显示图片缩略图
    C#项目版本号自定义位置自动向上增加小工具设计与实现
    父类和子类同名的变量和方法决定于哪个
    JdbcTemplate基本使用 增删该差API
    Tomcat结合Web Maven
  • 原文地址:https://www.cnblogs.com/Yimi/p/6003807.html
Copyright © 2020-2023  润新知