點擊事件在當前點擊位置彈出一個彈窗

<script type="text/javascript">
            //獲取鼠標位置GetPostion 
            function GetPostion(e) { 
            var x = getX(e); 
            var y = getY(e); 
            return [x,y]
            } 
            function getX(e) { 
            e = e || window.event; 
            return e.pageX || e.clientX + document.body.scrollLeft - document.body.clientLeft 
            } 
            function getY(e) { 
            e = e|| window.event; 
            return e.pageY || e.clientY + document.body.scrollTop - document.body.clientTop 
            } 
            
            $("table tr").click(function(){
                $(".cars-info-control").remove();
                var mousePos=GetPostion();
                var selfY=mousePos[1]-40;
                var selfX=mousePos[0]-78;
                $('<div class="btn-group cars-info-control"><a class="btn">調度</a> <a class="btn">當前位置</a> <a class="btn">視頻</a> </div>')
                .css("position","absolute").css("top",selfY).css("left",selfX).appendTo("body")
            })
        </script>

 上述方法,在FF下是無用 報錯(e is undifind),原來FF不支持直接的event,解決方法是不直接利用event屬性,用下面的代碼便可javascript

 function getEvent() //同時兼容ie和ff的寫法
        {  
            if(document.all)   return window.event;    
            func=getEvent.caller;        
            while(func!=null){  
                var arg0=func.arguments[0];
                if(arg0)
                {
                  if((arg0.constructor==Event || arg0.constructor ==MouseEvent) || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation))
                  {  
                  return arg0;
                  }
                }
                func=func.caller;
            }
            return null;
        }
 //獲取鼠標位置GetPostion 
         function GetPostion() {
              var e=getEvent();
              var x = getX(e);
              var y = getY(e);
              return [x, y]
           }

         function getX(e) {
               
             return e.pageX || e.clientX + document.body.scrollLeft - document.body.clientLeft
           }

         function getY(e) {
             
            return e.pageY || e.clientY + document.body.scrollTop - document.body.clientTop
          } 
相關文章
相關標籤/搜索