js恶补1

1.onmouseover和onmouseout的事件对应的HTML标签:
<a>, <address>, <area>, <b>, <bdo>, <big>, <blockquote>, <body>, <button>,
<caption>, <cite>, <code>, <dd>, <dfn>, <div>, <dl>, <dt>, <em>, <fieldset>,
<form>, <h1> to <h6>, <hr>, <i>, <img>, <input>, <kbd>, <label>, <legend>,
<li>, <map>, <ol>, <p>, <pre>, <samp>, <select>, <small>, <span>, <strong>,
<sub>, <sup>, <table>, <tbody>, <td>, <textarea>, <tfoot>, <th>, <thead>,
<tr>, <tt>, <ul>, <var>
2.很简单的try...catch语句:
<html>
<head>
<script type="text/javascript">
var txt=""
function message()
{
try
   {
   alert("Welcome guest!")//此处可以故意写错,然后就会执行catch的内容
   }
catch(err)
   {
   txt="本页中存在错误。\n\n"
   txt+="错误描述:" + err.description + "\n\n"
   txt+="点击“确定”继续。\n\n"
   alert(txt)
   }
}
</script>
</head>
<body>
<input type="button" value="查看消息" />
</body>
</html>
 
3.带有确认判断和跳转的try...catch语句
<html>
<head>
<script type="text/javascript">
var txt=""
function message()
{
try
   {
   adddlert("Welcome guest!")
   }
catch(err)
   {
     txt="本页中存在错误。\n\n"
     txt+="点击“确定”继续查看本页,\n"
     txt+="点击“取消”返回首页。\n\n"
     if(!confirm(txt))
         {
         document.location.href="/index.html"
         }
   }
}
</script>
</head>
<body>
<input type="button" value="查看消息" />
</body>
</html>
4.带有throw的异常处理
<html>
<body>
<script type="text/javascript">
var x=prompt("请输入 0 至 10 之间的数:","")
try
{
if(x>10)
  throw "Err1"
else if(x<0)
  throw "Err2"
else if(isNaN(x))
  throw "Err3"
}
catch(er)
{
if(er=="Err1")
  alert("错误!该值太大!")
if(er == "Err2")
  alert("错误!该值太大!")
if(er == "Err3")
  alert("错误!该值不是数字!")
}
</script>
</body>
</html>
5.捕捉错误的onerror,来捕捉网页错误,有三个参数:msg(错误信息)、url(链接地址)和line(发生错误的行)
<html>
<head>
<script type="text/javascript">
onerror=handleErr
var txt=""
function handleErr(msg,url,l)
{
txt="There was an error on this page.\n\n"
txt+="Error: " + msg + "\n"
txt+="URL: " + url + "\n"
txt+="Line: " + l + "\n\n"
txt+="Click OK to continue.\n\n"
alert(txt)
return true
}
function message()
{
adddlert("Welcome guest!")
}
</script>
</head>
<body>
<input type="button" value="View message" />
</body>
</html>
 

 

你可能感兴趣的:(js,职场,休闲)