0

这是我的代码——

<li>
<a  id="LoginTxt2" onmouseover="MouseOver(this);" onmouseout="MouseOut(this);" href="#">
<div style="background-color:#CCC;">OLD Text</div>
</a>
</li>

JS--

MouseOver = function(obj){
    var id = obj.id;
    document.getElementById(id).innerHTML='<div style="background-color:#DDD;">NEW Text</div>';

 }
MouseOut  = function(obj){
    var id = obj.id;
    document.getElementById(id).innerHTML="<div style="background-color:#CCC;">OLD Text</div>";

 }

当我的鼠标转到子 div 时,MouseOut fierd 我不想这样做...请帮助

4

1 回答 1

0

这是事件的工作方式,无法避免MouseOut()调用该函数。您可以在MouseOut()函数内部检查您现在悬停的元素是否是外部元素的子元素:

MouseOut = function(obj, event) {
    // this is the original element the event handler was assigned to
    var e = event.toElement || event.relatedTarget;

    // check for all children levels (checking from bottom up)
    while (e && e.parentNode && e.parentNode != window) {
        if (e.parentNode == this||  e == this) {
            if(e.preventDefault) {
                e.preventDefault();
            }
            return false;
        }
        e = e.parentNode;
    }
    var id = obj.id;
    document.getElementById(id).innerHTML="<div style="background-color:#CCC;">OLD Text</div>";
}

请注意,您需要将onmouseout声明更改为onmouseout="MouseOut(this,event);".

注意:来自https://stackoverflow.com/a/13141057/1417546的解决方案

于 2013-05-08T10:44:32.520 回答