Javascript onmouseout問題解決方案
我們預期只有當鼠標從div中移開的時候纔會觸發onmouseout事件,可是,事實上,當我們移到div中的元素時就會觸發onmousout事件。也就是說,移到對象的子對象上,也算onmouseout了。這往往會讓我們預期的效果達不到。此方法可解決這個問題。
<script type="text/javascript">
function test(obj, e) {
if (e.currentTarget) {
if (e.relatedTarget != obj) {
//e.relatedTarget爲event的相關節點
if (obj != e.relatedTarget.parentNode) {
alert(1);
}
}
} else {
if (e.toElement != obj) {
if (obj != e.toElement.parentNode) {
alert(1);
}
}
}
}</script><div onmouseout="test(this, event)" style="width:100px;height:100px;border:1px #666 solid"><span style="margin:5px;width:100%;height:100%;border:1px #ff0000 solid">faddsf</span></div></body></html>
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/LLorJJ999/archive/2009/10/29/4741232.aspx