美文网首页
JS基础整理 - 4

JS基础整理 - 4

作者: 我就是个伪程序媛 | 来源:发表于2016-08-15 14:43 被阅读18次

    1函数就是包裹在花括号中的代码块,前面使用了关键词 function,例如:

    function functionname()

    {

    这里是要执行的代码

    }

    2多参函数,例如:

    点击这里function myFunction(name,job){alert("Welcome " +name+ ", the " +job);}

    3如果把值赋给尚未声明的变量,该变量将被自动作为全局变量声明。

    4 如果把数字与字符串相加,结果将成为字符串。

    5 不同类型的循环

    (1) for  循环代码块一定的次数

    (2) for/in 循环遍历对象的属性

    (3) while 当指定的条件为true时循环指定的代码块

    (4) do/while 同样当指定的条件为true时循环指定的代码块

    6 break语句用于跳出循环,continue用于跳过循环中的一个迭代。

    7 JavaScript标签

    (1) continue 语句(带有或不带标签引用)只能用在循环中。

    (2) break 语句(不带标签引用),只能用在循环或 switch 中。

    (3) 通过标签引用,break 语句可用于跳出任何 JavaScript 代码块。

    8 JavaScript错误

    (1) try语句测试代码块的错误。

    允许我们定义在执行时进行错误测试的代码块。

    (2) catch语句处理错误。

    定义当try代码块发生错误时,所执行的代码块。

    (3)throw语句创建自定义错误。

    (4) JavaScript语句try和catch是成对出现的。

    var txt=""; function message() { try {adddlert("Welcome guest!"); } catch(err) { txt="There was an error on this page.\n\n"; txt+="Error description: " + err.message + "\n\n"; txt+="Click OK to continue.\n\n"; alert(txt); } }  

    (5) 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

    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

    My First JavaScript

    Please input a number between 5 and 10:

    Test Input

    (6)JavaScript 可用来在数据被送往服务前对HTML表单中的这些输入数据进行验证,被 JavaScript 验证的这些典型的表单数据有:

    用户是否已填写表单中的必填项目?

    用户输入的邮件地址是否合法?

    用户是否已输入合法的日期?

    用户是否在数据域 (numeric field) 中输入了文本?

    For Example One:

    Email:   

    For Example Two Email验证

    (1) 输入的数据必须包含@符号和点号(.)

    (2) @不可以是邮件地址的首字符,且@后面必须要有一个(.)

    function validate_email(field,alerttxt) { with (field) { apos=value.indexOf("@") dotpos=value.lastIndexOf(".") if (apos Email:

    相关文章

      网友评论

          本文标题:JS基础整理 - 4

          本文链接:https://www.haomeiwen.com/subject/piyusttx.html