zoukankan      html  css  js  c++  java
  • js02--对象、函数、switch、for、异常、表单验证

    现在我们接着来继续学习有关js的一些基础。

    1.undefined与null
        undefined:当变量声明但尚未赋值时,它的类型就是undefined
        null:表示一个不存在的对象,它的类型是object
        
    2.对象:
        js中的所有事物都是对象:字符串、数字、数组、日期。对象是拥有属性与方法的数据

        创建对象:
        var person=new Object();
        person.firstName="Bill";
        person.lastName="Gates";
        person.age=56;
    

     
    3.函数:
        function functionName(){...}
        
        函数局部变量:在函数内部被声明的变量,只能在函数内部访问,函数执行完毕,即被删除
        全局变量:在函数外声明的变量,网页上的所有脚本和函数都可以访问它
        变量的生存期:变量的生存期从声明时开始,局部变量在函数执行完毕即删除,全局变量在网页关闭时删除
        将值赋给尚未声明的变量,该变量自动变成全局变量
        
    4.switch语句case子块需要使用break,阻止继续执行下一个case

    5.for语句:

        var cars=["BMW","Volvo","Saab","Ford"];
        for(var i=0;i<cars.length;++i){
            document.write(cars[i]+"<br />");
        }
    

        
    6.九九乘法:

        var result=0;
        for(var i=1;i<=9;++i){
            for(var j=1;j<=i;++j){
                resu=j*i;
                document.write(j+"*"+i+"="+result+" ");
            }
        document.write("<br />");
        }
    

     
    7.for in:遍历对象属性或数组(不建议用于遍历数组)

        var person={firstName:"Bill",lastName:"Gates",age:56}
        for(var x in person){person[x]}
    

        
    8.break    continue
        break:跳出本层循环
        continue:跳出本次迭代

    9.按钮

        <input type="button" value="查看消息" onclick="message()" />
        <button type="button" onclick="myFunction()">测试输入值</button>
    

        
    10.js错误:try{}catch{},还可以throw异常。异常可以是字符串、数字、布尔值、对象

        function myFunction(){
            try{
                var x=document.getElementById("demo").value;
                if(""==x) throw "Empty!";
                if(isNaN(x)) throw "not a number";
                if(x>10)    throw "too high";
                if(x<5)        throw "too low";
            }
            catch(err){
                var y=document.getElementById("mess");
                y.innerHTML="ERROR:"+err+"!";
            }
        }
    

        
    11.表单验证:表单数据在送往服务器前进行验证

        <form action="submitPage.html" onsubmit="return validateForm(this);" method="post">
            Email:<input type="text" name="email" size="30" /> <br />
            <input type="submit" value="Submit" />
        </form>
        
        <script type="text/javascript">
        function validate_email(field,alerttxt)
        {
            with (field)
            {
            apos=value.indexOf("@")
            dotpos=value.lastIndexOf(".")
            if (apos<1||dotpos-apos<2)
            {alert(alerttxt);return false}
            else {return true}
            }
        }
    
        function validate_form(thisform)
        {
            with (thisform)
            {
                if (validate_email(email,"Not a valid e-mail address!")==false)
                {email.focus();return false}
            }
        }
        </script>
    
  • 相关阅读:
    一些 SQLite技巧
    linux增加swap空间
    linux解压命令
    数据库常用语句
    服务器命令
    Clickhouse高可用配置总结
    MySQL笔记
    Linux查看硬件信息
    Greenplum安装
    ClickHouse学习笔记
  • 原文地址:https://www.cnblogs.com/kuai-man/p/10711404.html
Copyright © 2011-2022 走看看