zoukankan      html  css  js  c++  java
  • JavaScript学习

    控制语句

      if-else 语句

    <script>
        //控制语句
            //if-else格式:
        var x = 1
            if(x==1){
                console.log("this is if")
            }else if(x>1){
                console.log("this is else if")
            }else {
                console.log("this else")
            }
    </script>

      switch语句

    <script>
        //switch语句
        var x = 8;
        switch (x){
            case 1:alert("this 1");break;
            case 2:alert("this 2");break;
            case 3:alert("this 3");break;
            case 4:alert("this 4");break;
            case 5:alert("this 5");break;
            case 6:alert("this 6");break;
            case 7:alert("this 7");break;
            default:alert("nothing")
        }
    </script>

      for循环语句

    <script>
        // for循环:for while
        // 基本格式:
        for(初始化;条件;增量){语句}
        for(i=1;i<100;i++){
            document.write(i)
            document.write('<br>')
        }
    
        var x = [1,2,3,4,5,6]
        for(i=0;i<x.length;i++){
            document.write(x[i])
            document.write('<br>')
        }
    
    
        //不建议使用
        var x = ['a','b','c','d']
        for(i in x){
            document.write('this i,输出:'+i);
            document.write('<br>');
            document.write('this x[i],输出:'+x[i]);
            document.write('<br>');
            document.write('----------------------');
            document.write('<br>');
        }
        // this i,输出:0
        // this x[i],输出:a
        // ----------------------
        // this i,输出:1
        // this x[i],输出:b
        // ----------------------
        // this i,输出:2
        // this x[i],输出:c
        // ----------------------
        // this i,输出:3
        // this x[i],输出:d
        // ----------------------
    
      
      <body>
       <p>A</p>
      <p>B</p>
      <p>C</p>
      <p>D</p>
      <p>E</p>
      <p>F</p>
      <p>G</p>
      </body>
      //不建议使用的原因
        var x2 = document.getElementsByTagName('p')
        for(i in x2) {
            document.write('this i,输出:' + i);
            document.write('<br>');
            document.write('this x[i],输出:' + x2[i]);
            document.write('<br>');
            document.write('----------------------');
            document.write('<br>');
        }
        // this i,输出:0
        // this x[i],输出:[object HTMLParagraphElement]
        // ----------------------
        // this i,输出:1
        // this x[i],输出:[object HTMLParagraphElement]
        // ----------------------
        // this i,输出:2
        // this x[i],输出:[object HTMLParagraphElement]
        // ----------------------
        // this i,输出:3
        // this x[i],输出:[object HTMLParagraphElement]
        // ----------------------
        // this i,输出:4
        // this x[i],输出:[object HTMLParagraphElement]
        // ----------------------
        // this i,输出:5
        // this x[i],输出:[object HTMLParagraphElement]
        // ----------------------
        // this i,输出:6
        // this x[i],输出:[object HTMLParagraphElement]
        // ----------------------
        // this i,输出:length
        // this x[i],输出:7
        // ----------------------
        // this i,输出:item
        // this x[i],输出:function item() { [native code] }
        // ----------------------
        // this i,输出:namedItem
        // this x[i],输出:function namedItem() { [native code] }
    </script>

      while循环语句

    <script>
        //while 循环
        var i=0;j=0;
        while(i<201){
            j +=i
            i++;
        }
        document.write(j)
    </script>

    异常处理

    <script>
        //异常
        //try{主程序}catch(异常){执行}finally{无论是否异常都执行}
        //主动抛出异常 throw Error("xxxx")
        try{
            throw Error("报错了!!!")
            document.write("OK")
        }
        catch (e) {
            document.write("异常")
        }
        finally {
            document.write("OK and OK")
        }
    </script>
  • 相关阅读:
    ZOJ 2588 Burning Bridges
    POJ 1966 ZOJ 2182 Cable TV Network
    HDU 5348 MZL's endless loop
    HDU 5352 MZL's City
    Tarjan算法求解无向连通图的割点、割边、点双连通分量和边双连通分量的模板
    ZOJ 1119 SPF
    HDU 3452 Bonsai
    HDU 1520 Anniversary party
    POJ 2239 Selecting Courses
    POJ 1144 Network
  • 原文地址:https://www.cnblogs.com/Anec/p/9826587.html
Copyright © 2011-2022 走看看