Posts Tagged ‘try’

使用try,catch防止页面出错.

Posted in 默认分类 on 六月 7th, 2010 by admin – Be the first to comment
jQuery(function(){
	try{
		$(':input').each(function(i){
			var t = $(this).attr('type');
			if(t==='button' || t==='submit'){
				$(this).css({'cursor':'pointer'});
			}
		});
	}catch(e){}

});

javascript异常处理实例

Posted in 前端技术 on 十月 14th, 2008 by admin – Be the first to comment

以前一直不知道如何使用异常处理。今天在处理一个项目时用到,于是看了一下手册,写了一个简单示例.

<div id=”a”>abc</div>
<script>
  try {
   var x = document.getElementById(“ab”); 
   alert(x.innerHTML) //这句会出错,因为没有找到 ab这个ID
  }
  catch(e) {
   alert(“出错:”+e.description); //显示出错的原因
  }
</script>

其实很简单,汗!