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

推荐订阅源

S
Secure Thoughts
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Securelist
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
SegmentFault 最新的问题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
K
Kaspersky official blog
WordPress大学
WordPress大学
I
Intezer
L
Lohrmann on Cybersecurity
V
Vulnerabilities – Threatpost
C
Check Point Blog
A
About on SuperTechFans
AWS News Blog
AWS News Blog
Latest news
Latest news
宝玉的分享
宝玉的分享
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cyber Attacks, Cyber Crime and Cyber Security
SecWiki News
SecWiki News
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
A
Arctic Wolf
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
V
V2EX
Scott Helme
Scott Helme
I
InfoQ
Project Zero
Project Zero
Security Archives - TechRepublic
Security Archives - TechRepublic
Recent Announcements
Recent Announcements
Spread Privacy
Spread Privacy
Attack and Defense Labs
Attack and Defense Labs
大猫的无限游戏
大猫的无限游戏
Webroot Blog
Webroot Blog
N
News and Events Feed by Topic
博客园 - 司徒正美
Microsoft Security Blog
Microsoft Security Blog
The GitHub Blog
The GitHub Blog
NISL@THU
NISL@THU
L
LangChain Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - 狼问苍穹

php图片、文件上传 怎样推广自己的博客 nfs文件系統 重新来过! 网站被挂iframe木马的解决方案 mysql截取函数LOCATE和POSITION PHP公历农历转换(阴历阳历转换)阴历和阳历转换 去掉fck里的图片 js 对FCKeditor 进行是否为空验证 Zend studio 打开 utf-8 出现乱码之解决办法 JavaScript事件监听完整实例 兼容IE、Firefox的DIV透明 阻止JavaScript事件冒泡传递 date picker plugin - jQuery 用法 Transact-SQL语句总汇(转载) 建一个临时表 php数组总结资料 js获取url参数 smarty date_format 产生乱码??
解决HTML内部元素的Mouse事件干扰(实例,兼容ff,ie)
狼问苍穹 · 2009-01-16 · via 博客园 - 狼问苍穹

原理:

话说有一个DIV元素,其内部有一个IMG元素和SPAN元素,不用理会这两个内部元素怎么布局,这不是我要讨论的重点。
为了实现一些特殊的效果,我需要利用TD的onmouseover和onmouseout事件,测试时就会发现如下的状况:
当鼠标移入DIV内部时,onmouseover事件被触发;接着再鼠标移动到DIV内部的IMG或者SPAN元素之上,我们肯定不会认为这时鼠标已经移到了DIV的外边,但奇怪的是onmouseout事件触发了,而且紧接着onmouseover事件也马上被触发了。
这可不是我想要的,那么怎么来“屏蔽”内部元素给外层元素带来的Javascript事件干扰呢?
这里列举两种方法:

一. setTimeout

因为在鼠标移动到内部元素之上而触发了外层元素的onmouseout事件后,外层元素的onmouseover也会马上触发,所以我们只需要把外层元素的onmouseout事件需要执行的动作延迟很短的一段时间来运行,然后在onmouseover事件中再执行clearTimeout方法,这样就可以避免内部元素引起的事件干扰。
具体的执行过程请看下图(纵向的虚线表示时间):

这是个很巧妙的的方法,因为当onmouseout触发后,实质性的方法并没有马上执行,而是要等待一小段时间。如果在这段时间里马上又触发了 onmouseover事件,那么基本上就可以肯定onmouseout事件的触发是因为内部元素的干扰了,所以在onmouseover事件中使用 clearTimeout来阻止延时的方法执行。

二.contains

在onmouseover时先进行如下判断,结果为true时再执行方法体:
if(!this.contains(event.fromElement)){MouseOverFunc()}
在onmouseout时先进行如下判断,结果为true时再执行方法体:
if(!this.contains(event.toElement)){MouseOutFunc()}
下面来解释一下上面两行代码的含义:
在IE中,所有的HTML元素都有一个contains方法,它的作用是判断当前元素内部是否包含指定的元素。我们利用这个方法来判断外层元素的事件是不是因为内部元素而被触发,如果内部元素导致了不需要的事件被触发,那我们就忽略这个事件。
event.fromElement指向触发onmouseover和onmouseout事件时鼠标离开的元素;event.toElement指向触发onmouseover和onmouseout事件时鼠标进入的元素。
那么上面两行代码的含义就分别是:
○ 当触发onmouseover事件时,判断鼠标离开的元素是否是当前元素的内部元素,如果是,忽略此事件;
○ 当触发onmouseout事件时,判断鼠标进入的元素是否是当前元素的内部元素,如果是,忽略此事件;
这样,内部元素就不会干扰外层元素的onmouseover和onmouseout事件了。
但问题又来了,非IE的浏览器并不支持contains函数,不过既然我们已经知道了contains函数的作用,就可以自行添加如下的代码来为非IE浏览器增加contains支持:

if(typeof(HTMLElement) != "undefined")
{
HTMLElement.prototype.contains 
= function(obj) 
{   
          
while(obj != null &&  typeof(obj.tagName) != "undefind")   
          {
if(obj == this)
Return 
true;
Obj 
= obj.parentNode;
}    
return false;   
};   
}

实例代码:可以兼容ff

 1function out(id, event)
 2{
 3    var parent = document.getElementById(id)
 4    if(typeof(HTMLElement)!="undefined")    //给firefox定义contains()方法,ie下不起作用
 5    {
 6          HTMLElement.prototype.contains=function(obj)
 7          
 8              while(obj!=null&&typeof(obj.tagName)!="undefind")//通过循环对比来判断是不是obj的父元素
 9                 if(obj==thisreturn true;
10               obj=obj.parentNode;
11               }
 
12              return false;
13          }
;  
14    }
  
15    if ($.browser.mozilla) 
16    {
17            if (!parent.contains(event.relatedTarget)) //如果是子元素
18                      $("#"+id).fadeOut(3000function(){
19                            $("#"+id+" .todaydiary_list").hide();
20                        }
);
21            }

22    }
 
23    else
24    {
25         if (!parent.contains(event.toElement)) {
26                 $("#"+id).fadeOut(3000function(){
27                    $("#"+id+" .todaydiary_list").hide();
28                }
);
29        }

30    }

31}