<div onmouseout='mouseOut()' id="div1">2222<a>111</a></div>function mouseOut(){
document.getElementById("div1").style.display="none";
}我要求鼠标移出div时,才执行mouseOut方法。但我鼠标在div内移动,当移动到<a>里面时,他也执行mouseOut方法。怎么控制,只要鼠标在div内,均不执行mouseOut方法呢????(最好是直接设置div的属性,或事件的属性能控制)

解决方案 »

  1.   

    阻止冒泡
    http://topic.csdn.net/u/20090929/21/314549cd-adcf-41a1-91b2-4eaf2ae0aebf.html<script>
    if(typeof(HTMLElement)!="undefined")
    HTMLElement.prototype.contains=function(obj)
    {
      if(obj==this)return true;
      while(obj=obj.parentNode) if(obj==this) return true;
      return false;
    }
    function doit(evt){
        evt = window.event?window.event:evt;
        var obj = evt.toElement || evt.relatedTarget
        if(document.getElementById("divPromptBox").contains(obj)) return;
        alert('');
    }
    </script>
    <div id="divPromptBox" class="PromptBox" style="width:50%;background:#ccc" onmouseout="doit(event);">
    <a href="#">... </a>
    <div>... <div>
    <span>... </span> 
    </div> 
      

  2.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
     <head>
      <title> New Document </title>
      <meta name="Generator" content="EditPlus">
      <meta name="Author" content="">
      <meta name="Keywords" content="">
      <meta name="Description" content="">
     </head> <body>
      
    <script type="text/javascript">
    <!--
    if(typeof(HTMLElement)!="undefined")
    HTMLElement.prototype.contains=function(obj)
    {
      if(obj==this)return true;
      while(obj=obj.parentNode) if(obj==this) return true;
      return false;
    }
    function doit(evt){
        evt = window.event?window.event:evt;
        var obj = evt.toElement || evt.relatedTarget
        if(document.getElementById("divPromptBox").contains(obj)) return;
        document.getElementById("divPromptBox").style.display = "none";
    }//-->
    </script>
    <div id="divPromptBox" class="PromptBox" style="width:50%;background:#ccc" onmouseout="doit(event);">
    <a href="#">... </a>
    <div>... <div>
    <span>... </span> 
    </div> 
     </body>
    </html>
    楼上的,我学习了。
      

  3.   

    这是最简单的方法var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
    function doMouseOut(e, father) {
        var event = e || window.event;
        var parent = isIE ? event.toElement : event.relatedTarget;
        while ( parent && parent !== father ) {
            parent = parent.parentNode;
        }
        if ( parent !== father ) {
            // 你的函数
            mouseOut();
        }
    }<div onmouseout='doMouseOut(event, this);' id="div1">2222<a>111</a></div>
      

  4.   

    主要是防止冒泡,楼主可以在网上查一下,这个方面jquery做的比较好,js比较繁琐