fireEvent

摘自果果的js库:

         function addEvent(el,type,fn,capture){
		if(el.addEventListener){
			el.addEventListener(type,fn,capture);
		}else if(el.attachEvent){
			el.attachEvent('on'+type,function(){
				fn.call(el);
			});
		}else{
			el['on'+type] = fn;
		}
	}
	function fireEvent(el,type){
		if(document.createEventObject){
			return el.fireEvent('on'+type);
		}else{
			var e = document.createEvent('HTMLEvents');
			e.initEvent(type,true,true);
			return !el.dispatchEvent(e);
		}
	}
	var a = document.getElementById('a'),b = document.getElementById('b');
	addEvent(a,'click',function(){alert('a has been clicked')});
	addEvent(a,'mouseover',function(){alert('mouseover')});
	addEvent(b,'click',function(){fireEvent(a,'click')});

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>project index</title>
</head>
<body>
	<div id="a">aaaaa</div>
	<div id="b">bbbbb</div>
</body>
</html>

posted @ 2010-10-21 15:41  zjhsd2007  阅读(301)  评论(0编辑  收藏  举报