当前位置: 代码迷 >> JavaScript >> ,关于event
  详细解决方案

,关于event

热度:353   发布时间:2012-03-16 16:34:56.0
求助,关于event
当前鼠标坐标为:
X: <span   id= "mp_x "> </span>
Y: <span   id= "mp_y "> </span>
<div   id= "Container "   onmouseover=DisplayMp(event)     style= "border:1px   dashed   #ff9900;height:200px; ">

</div>
<script   type= "text/javascript ">
window.onload   =   function(){
DisplayMp   =   function(event){
document.getElementById( "mp_x ").innerHTML   =   event.clientX   +   "px ";
document.getElementById( "mp_y ").innerHTML   =   event.clientY   +   "px ";
}  
document.getElementById( "Container ").onmouseover   =   function(event){
document.getElementById( "mp_x ").innerHTML   =   event.clientX   +   "px ";
document.getElementById( "mp_y ").innerHTML   =   event.clientY   +   "px ";
}
}
</script>  

//----------------------
上面两种写法   前面那种写法没问题
后面那种写法不能正常取到坐标,请问该怎么做?

------解决方案--------------------
X: <span id= "mp_x "> </span>
Y: <span id= "mp_y "> </span>
<div id= "Container " onmouseover=DisplayMp(event) style= "border:1px dashed #ff9900;height:200px; ">

</div>
<script type= "text/javascript ">
window.onload = function(){
DisplayMp = function(event){
document.getElementById( "mp_x ").innerHTML = event.clientX + "px ";
document.getElementById( "mp_y ").innerHTML = event.clientY + "px ";
}
document.getElementById( "Container ").onmouseover = function(){
document.getElementById( "mp_x ").innerHTML = window.event.clientX + "px ";
document.getElementById( "mp_y ").innerHTML = window.event.clientY + "px ";
}
}
</script>

加个window.event就可以了
------解决方案--------------------
ie里event是window的成员,在ff中,是方法的参数。
所以要这样来取
function onmouseover(ev)
{
var e = ev || window.event;//这样来取得event对象
}
上面只是个例子自己根据实际情况改改。
------解决方案--------------------
Object.extend = function (a, b) {
//追加方法
for (var i in b) a[i] = b[i];
return a;
};

Object.extend(Object, {

addEvent : function (a, b, c, d) {
//添加函数
if (a.attachEvent) a.attachEvent(b[0], c);
else a.addEventListener(b[1] || b[0].replace(/^on/, " "), c, d || false);
return c;
},

delEvent : function (a, b, c, d) {
if (a.detachEvent) a.detachEvent(b[0], c);
else a.removeEventListener(b[1] || b[0].replace(/^on/, " "), c, d || false);
return c;
},

reEvent : function () {
//获取Event
return window.event ? window.event : (function (o) {
do {
o = o.caller;
} while (o && !/^\[object[ A-Za-z]*Event\]$/.test(o.arguments[0]));
return o.arguments[0];
})(this.reEvent);
}

});

var reMouse = function (a) {
//获取鼠标位置
var e = Object.reEvent();
return {
x : document.documentElement.scrollLeft + e.clientX,
y : document.documentElement.scrollTop + e.clientY
};
};
------解决方案--------------------
  相关解决方案