js获取触发事件元素的坐标

 

function Point(iX, iY){
    this.x = iX;
    this.y = iY;
}

 

function fGetXY(aTag){    
    var oTmp=aTag;
    var pt = new Point(0,0);
    do {        
        pt.x += oTmp.offsetLeft;
        pt.y += oTmp.offsetTop;
        oTmp = oTmp.offsetParent;
    } while(oTmp.tagName!="BODY");
    
    return pt;
}

 

如果想在触发事件的元素(例如文本框假如是myinput)下面弹出一个div

        获取触发事件的元素

       var myinput = window.event.srcElement;

        设置弹出元素的位置

        div.left = point.x; 
        div.top = point.y+myinput.offsetHeight+1; 
        div.style.visibility = 'visible';

你可能感兴趣的:(js)