异常捕获

Try{

  发生异常的代码块

}catch(err){

  异常信息处理

}

 <!DOCTYPE html>
<html>
<head lang="en">
<meta chaset="UTF-8">
<title></title>
</head>
<body>
<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 "请输入"; //一般throw会与try,catch配合使用
}
}catch(err){
alert(err);
}
}
</script>
</body>
</html>

事件处理

1、onclick鼠标点击事件

2、onmouseout鼠标离开事件

3、onmouseover鼠标经过事件

4、onchange文本框内容改变事件

5、onselect文本框内容选中事件

6、onfocus光标聚集事件

7、onload网页加载完毕事件

样式表style.css

 .div{
width: 100px;
height: 100px;
background-color: red;
}
 <!DOCTYPE html>
<html>
<head lang="en">
<meta chaset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="style.css"> <!--指定rel为样式表,类型为css,外部文件为style.css-->
</head>
<body onload="onLoad()">                      <!--onload网页加载完毕事件-->
<button onclick="onClick()">按钮</button>         <!--onclick鼠标点击事件-->
<div class="div" onmouseout="onOut(this)" onmouseover="onOver(this)"></div> <!--onmouseout鼠标离开事件,onmouseover鼠标经过事件 -->
<form>
<input type="text" onchange="onChange(this)">     <!--onchange文本框内容改变事件-->
<br>
<input type="text" onselect="onSelect(this)" onfocus="onFocus(this)"> <!--onselect文本框内容选中事件,onfocus光标聚集事件-->
</form>
<script>
function onClick(){
alert("onclick鼠标点击");
}
function onOver(ooj){
ooj.innerHTML="onmouseover鼠标经过";
}
function onOut(ooj){
ooj.innerHTML="onmouseout鼠标离开";
}
function onChange(bg){
alert("onchange文本框内容改变");
}
function onSelect(bg){
bg.style.background="yellow";
alert("onselect文本框内容选中");
}
function onFocus(bg){
bg.style.background="green";
alert("onfocus光标聚集");
}
function onLoad(){
alert("onload网页加载完毕");
}
</script>
</body>
</html>
04-18 16:47
查看更多