如果我有这样的事情:
有没有办法在BOTH div上触发mouSEOver事件?
编辑:对不起所有..我试图简化这个问题所以很清楚理解,我忘了提到我有超过100个这样的div那么可能那些解决方案不起作用.我要看看我是否可以对它们进行调整.非常感谢大家.
解决方法
我在这里用JSFiddle编写了一个工作示例:
http://jsfiddle.net/gfosco/CU5YT/
它类似于madeinstefanos的回答,但具体到你的例子..
var mouseX = 0; var mouseY = 0; var front = 0; var back = 0; function log(text) { $("#log").append(text + '<BR>'); } function mouseWithin(selector) { var pos = $(selector).position(); var top = pos.top; var left = pos.left; var height = $(selector).height(); var width = $(selector).width(); if (mouseX >= left && mouseY >= top && mouseX <= left + width && mouseY <= top + height) { return true; } return false; } $(document).bind('mousemove',function(e) { mouseX = e.pageX; mouseY = e.pageY; if (front == 1 && !mouseWithin("#front")) { front = 0; log('Front Leave'); } if (back == 1 && !mouseWithin("#back")) { back = 0; log('Back Leave'); } if (front === 0 && mouseWithin("#front")) { front = 1; log('Front Hover'); } if (back === 0 && mouseWithin("#back")) { back = 1; log('Back Hover'); } });