JavaScript学习(二)基础知识

      

 变量类型:获取变量类型: alert(typeof  变量名);

 undefined(未定义):1,没有定义过该变量 2,没有给该变量赋值

 类型转换:

1,string转换为int:parseInt(string类型变量);当变量内容不是数字时,转换为NaN(非数字)

判断变量是否为数字:

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<script type="text/javascript">
    window.onload=function ()
    {
        
        var button1=document['getElementById']('button1');
        button1.onclick=function()
        {
            var text1=document['getElementById']('text1').value;
            var n=parseInt(text1);
            if(isNaN(n))
                             {
                             alert(n+'不是数字');

                             }
                            else
                          alert(n+'输入是数字');
        }
                 
    }
</script>
<body>
    <input type="text" id="text1">
    <input type='button' id="button1" value="判断">
</body>
</html>

2,string转换为float:parseFloat(string类型变量)

猜你喜欢

转载自www.cnblogs.com/lq13035130506/p/12195484.html
今日推荐