惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
博客园 - 【当耐特】
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
D
Docker
T
The Blog of Author Tim Ferriss
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
罗磊的独立博客
小众软件
小众软件
A
About on SuperTechFans
MyScale Blog
MyScale Blog
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
C
Check Point Blog
L
LangChain Blog

博客园 - 李帅斌-Memory

快速导出CSV log4net使用详解 . C# 格式化字符串 web.config中特殊字符的转译 transactionscope的使用 一些概念需要明确的解释一下 还原已清空的回收站 安装卸载Windows服务 Jquery 文章积累 Spring.NET学习——控制反转(基础篇) SqlServer2005导出数据到SqlServer2000步骤! - 李帅斌-Memory - 博客园 MSSQL2005:SQL Server 2005安装图解 网站优化-Gzip压缩 Asp.Net读取excel文件的时候 出错提示:外部表不是预期的格式 解决方案 js触发回车事件 - 李帅斌-Memory - 博客园 VisualStudio用IE8调试时遇到的问题 JQuery+Ajax实现无刷新数据查询 Asp.net页面执行两遍Page_Load的解决方案 C#取硬盘序列号(-备忘-)
在Div中包含的元素移动时,触发div的onmouseout事件.解决方案!...
李帅斌-Memory · 2009-08-06 · via 博客园 - 李帅斌-Memory

我们在做显示隐藏层时,需要使用到onmouseover、onmouseout事件。
问题如下,当层中包含其它元素时,例如层中包含若干个子层,
<div id="dvroot">
      <div id="dvsub1"></div>
      <div id="dvsub1"></div>
</div>
鼠标从dvroot的空白区域,移动到dvsub层中时,我们的要求是dvroot层继续显示,但是,在移动到dvsub时,触发了onmouseout的事件,这时dvroot就会隐藏。针对该问题,在网上搜索了一个完美的解决方案,现记录下来。

以下是做的demo示例,在IE、FF下测试通过。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>javascript div mouseout-hidden-zone</title>

<script>
(function (bool) {
//兼容FF一些方法
    var html;
   
    if (bool) {
   
        html = window.HTMLElement.prototype;
       
        window.__defineGetter__("event", function () {
        //兼容Event对象
            var o = arguments.callee;
           
            do {
           
                if (o.arguments[0] instanceof Event) return o.arguments[0];
           
            } while (o = o.caller);
           
            return null;
       
        });
       
        Event.prototype.__defineGetter__("fromElement", function () {
            return this.relatedTarget;
        });
       
        html.contains = function (o) {
            do {
                if (o == this) return true;
            } while (o = o.parentNode);
            return false;
        };
       
    }
 
})(/Firefox/.test(window.navigator.userAgent));

function hitOneMoreTime(){
  var dvBox = document.getElementById("dvBox");
  
  dvBox.style.display = '';
  dvBox.onmouseout = function(){
   var dvBox = this, e = window.event;
   if(!dvBox.contains(e.toElement || e.fromElement))
   {
    dvBox.style.display = 'none';
   }
  }
 }
</script>
</head>
<body>
<div style="width: 80px; height:30px;border: solid 1px #000;" onclick="hitOneMoreTime()">Click</div>
<div id="dvBox" style="display:none;text-align:center; width: 500px; height:500px; border: solid 1px #d7d; ">
<div><img src='http://www5.mydavinci.com/x/2092/ST' /></div>
</div>
</body>
</html>

 希望对遇到这个问题的朋友有帮助。