点击事件在当前点击位置弹出一个弹窗
<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属性,用下面的代码即可
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 }