函数应用

定义函数
function 函数名
{
函数体;//代码块
}
注意大小写声明,在script标签中可以定义多个函数方法。
函数不会自动执行,需要进行调用。可以在

<head runat="server">         
   <script>
         function AddValue(a,b)
         {
            var sum = a+b;   //局部变量
            //return sum;    //带返回值
            //x=5;           //全局变量
            alert(sum);
         }
        // alert(x);  可全局使用
         
         function galary(name,money)
         {
            alert("你好:" + name + ",工资为:" + money);
         }
        // galary('井队',6666)     //标签内调用,注意刷新时会再次调用
     </script></head>
<body>
    <form id="form1" runat="server">
    <div>
     <button onclick="AddValue(1,2)">按钮</button>  
    </div>
    </form>
</body>
</html>

异常捕获

  1. 异常: 当JavaScript引擎执行JavaScript代码时,发生错误,导致程序停止运行
  2. 异常抛出:当异常产生,并且将这个异常生成一个错误信息。
  3. 异常捕获:
    Try{ 发生异常的代码块;}
    Catch(err){ 错误信息处理;}
  4. Throw语句:创建一个自定义的错误。
  <form>
     <input id = "txt" type ="text" />
     <input id = "btn" type ="button" onclick="demo()" value ="按钮"/>
    
     </form>
     <script>
       function demo(){
          try
          {
            var e = document.getElementById("txt").value;
            if(e =="")
                throw "请输入姓名";      
          }catch(err)
          {
            alert(err);
          }
       
       }
    </script>

JavaScript基础学习第二天

相关文章:

  • 2021-09-08
  • 2021-12-08
  • 2021-04-17
  • 2021-07-30
  • 2021-11-03
  • 2019-03-08
  • 2021-03-11
  • 2021-09-18
猜你喜欢
  • 2021-12-28
  • 2019-07-28
  • 2021-12-25
  • 2021-06-13
  • 2021-06-06
  • 2021-12-21
  • 2021-10-18
相关资源
相似解决方案