<!DOCTYPE html>
<html lang="zh">
<head>
<title>DOM功能扩展</title>
</head>
<body>
<a href="javascript:void(0)">你好</a>
<script type="text/javascript">
function DOMExtend(name, fn){
if(typeof(HTMLElement)!="undefined"){
eval("HTMLElement.prototype."+name+"="+fn);
}
else{
var _getElementById=document.getElementById;
document.getElementById=function(id){
var _elem=_getElementById(id);
eval("_elem."+name+"="+fn);
return _elem;
}
var _getElementByTagName=document.getElementsByTagName;
document.getElementsByTagName=function(tag){
var _elem=_getElementByTagName(tag);
var len=_elem.length;
for(var i=0;i<len;i++){
eval("_elem["+i+"]."+name+"="+fn);
}
return _elem;
}
var _createElement=document.createElement;
document.createElement=function(tag){
var _elem=_createElement(tag);
eval("_elem."+name+"="+fn);
return _elem;
}
var _documentElement=document.documentElement;
eval("_documentElement."+name+"="+fn);
var _documentBody=document.body;
eval("_documentBody."+name+"="+fn);
}
}
function BindEvent(event,fun){
if(this.addEventListener){
this.addEventListener(event,fun,false);
}
else{
var tag=this;
tag.attachEvent("on"+event,function(){
fun.apply(tag,arguments);//这里是关键
});
}
}
DOMExtend("bind",BindEvent);var wrap=document.getElementById("tagA");
wrap.bind("click",function(){
alert(this.innerHTML);
})
</script>
</body>
</html>
您可能感兴趣的文章: