代码摘自“泽元软件-ZCMS”,作为自己学习之用:
(转载请注明代码来自“泽元软件-ZCMS”)
var isIE=navigator.userAgent.toLowerCase().indexOf("msie")!=-1; var isIE6=navigator.userAgent.toLowerCase().indexOf("msie 6.0")!=-1; var isIE7=navigator.userAgent.toLowerCase().indexOf("msie 7.0")!=-1&&!window.XDomainRequest; var isIE8=!!window.XDomainRequest; var isGecko=navigator.userAgent.toLowerCase().indexOf("gecko")!=-1; var isQuirks=document.compatMode=="BackCompat"; function $(a){ if(typeof(a)=="string"){ a=document.getElementById(a); if(!a){ return null } }else{ return a; } } function getEvent(a){ return window.event||a } function stopEvent(a){ a=getEvent(a); if(!a){ return; } if(isGecko){ a.preventDefault(); a.stopPropagation() } a.cancelBubble=true; a.returnValue=false } function getEventPosition(evt){ evt = window.event || evt; var f={x:evt.clientX, y:evt.clientY}; var d, srcEle = (evt.srcElement ? evt.srcElement : evt.target); if(isGecko){ d = srcEle.ownerDocument.defaultView }else{ d = srcEle.ownerDocument.parentWindow } var a,c; while(d != d.parent){ if(d.frameElement){ pos2 = $E.getPosition(d.frameElement); f.x += pos2.x; f.y += pos2.y } a = Math.max(d.document.body.scrollLeft, d.document.documentElement.scrollLeft); c = Math.max(d.document.body.scrollTop, d.document.documentElement.scrollTop); f.x -= a; f.y -= c; d = d.parent } return f } var $E = {}; $E.getPosition=function(m){ m=m||this; m=$(m); var k=m.ownerDocument; if(m.parentNode===null||m.style.display=="none"){ return false } var l=null; var j=[]; var g; if(m.getBoundingClientRect){ g=m.getBoundingClientRect(); var c=Math.max(k.documentElement.scrollTop,k.body.scrollTop); var d=Math.max(k.documentElement.scrollLeft,k.body.scrollLeft); var b=g.left+d-k.documentElement.clientLeft; var a=g.top+c-k.documentElement.clientTop; if(isIE){ b--; a-- } return {x:b,y:a } }else{ if(k.getBoxObjectFor){ g=k.getBoxObjectFor(m); var h=(m.style.borderLeftWidth)?parseInt(m.style.borderLeftWidth):0; var f=(m.style.borderTopWidth)?parseInt(m.style.borderTopWidth):0; j=[g.x-h,g.y-f] } } if(m.parentNode){ l=m.parentNode }else{ l=null } while(l&&l.tagName!="BODY"&&l.tagName!="HTML"){ j[0]-=l.scrollLeft; j[1]-=l.scrollTop; if(l.parentNode){ l=l.parentNode }else{ l=null } } return{x:j[0],y:j[1]} }; $E.getPositionEx=function(c){ c=c||this; c=$(c); var f=$E.getPosition(c); var d=window; var a,b; while(d!=d.parent){ if(d.frameElement){ pos2=$E.getPosition(d.frameElement); f.x+=pos2.x; f.y+=pos2.y } a=Math.max(d.document.body.scrollLeft,d.document.documentElement.scrollLeft); b=Math.max(d.document.body.scrollTop,d.document.documentElement.scrollTop); f.x-=a; f.y-=b; d=d.parent } return f }; $E.getParent=function(a,b){ b=b||this; b=$(b); while(b){ if(b.tagName.toLowerCase()==a.toLowerCase()){ return $(b) } b=b.parentElement } return null }; $E.getParentByAttr= function(a,c,b){ b=b||this; b=$(b); while(b){ if(b.getAttribute(a)==c){ return $(b) } b=b.parentElement } return null }; $E.getTopLevelWindow= function(){ var a=window; while(a!=a.parent){ a=a.parent } return a };