2013-05-08 58 views
0

這裏是我的代碼 -的onmouseout效果....我不想這樣做,

<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我不想這麼做... plz幫助

回答

0

這是事件的方式,並且沒有辦法避免調用MouseOut()函數。你可以做的是檢查,MouseOut()功能裏面,如果你現在徘徊的元素是外部元素的子元素:你需要的onmouseout聲明更改爲onmouseout="MouseOut(this,event);"

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>"; 
} 

注意。

注:從https://stackoverflow.com/a/13141057/1417546得到的解

相關問題