我想在鼠标移出一个div时候触发一个事件,而事实上onmouseout在鼠标移到这个div里面的html控件也触发了该事件,下面是我的测试代码:$(document).ready(function(){
   $("#tdiv").mouseout(function(){
     alert("y");
   });
});
<div  id="tdiv" STYLE="border-style:solid;border-width:5pt; border-color:black; height:180px;">
<input type="text"/> <br>
<input type="button"/> <br>
<span>aaaaaaaa</span><br>
</div>
当鼠标还在这个div中的时候,鼠标经过div里面的文本框,按钮跟span都触发了mouseout事件,该怎么解决这个问题???

解决方案 »

  1.   

    加上return false;看看是不是你要的?$(document).ready(function(){
       $("#tdiv").mouseout(function(){
         alert("y");
           return false;
       });        
    });​
      

  2.   

    判断目标 是否 div 的子元素,是子元素不执行if(typeof(HTMLElement) != "undefined")  // 给非IE 浏览器添加方法 contains

        HTMLElement.prototype.contains = function(obj) 
        {   
          while(obj != null &&  typeof(obj.tagName) != "undefined")   
          { 
                if(obj == this) 
                  return true; 
                obj = obj.parentNode; 
          }    
                return false;   
        };   
    } div.onmouseout=function(e){
                    e = e||event; 
                    e = e.toElement || e.relatedTarget ; // 目标元素 IE toElement  FF:relatedTarget
                   if(typeof(e)!="undefined" &&!this.contains(e)){ //目标 非子元素
    // 此次执行 mouseout 代码
    alert("mouseout");
    }
                };