jQuery中mouseleave和mouseout的区别详解
很多人在使用jQuery实现鼠标悬停效果时,一般都会用到mouseover和mouseout这对事件。而在实现过程中,可能会出现一些不理想的状况。
先看下使用mouseout的效果:
<p>先看下使用mouseout的效果:</p>
<div id="container" style="width: 300px;">
<div id="title" style="cursor: pointer; background: #ccc;">使用了mouseout事件↓</div>
<div id="list" style="display: none; position: absolute; background:#fff; border: 1px solid #ccc; width: 298px;">
<div>第一行</div>
<div>第二行</div>
<div>第三行</div>
</div>
</div>
<p><script type='text/javascript'>
jQuery(document).ready(function($) {
$("#title").mouseover(function() {
var offset = $(this).offset();
$("#list").css({left: offset.left, top: offset.top+19}).show();
});
$("#container").mouseout(function() {
$("#list").hide();
});
});
</script>
第一行第二行第三行我们发现使用mouseout事件时,鼠标只要在下拉容器#list里一移动,就触发了hide(),其实是因为mouseout事件是会冒泡的,也就是事件可能被同时绑定到了该容器的子元素上,所以鼠标移出每个子元素时也都会触发我们的hide()。
从jQuery 1.3开始新增了2个mouse事件,mouseenter和mouseleave。与mouseout事件不同,只有在鼠标指针离开被选元素时,才会触发 mouseleave 事件。
我们来看一下mouseleave事件的效果:
<div id="container2" style="width: 300px;">
<div id="title2" style="cursor: pointer; background: #ccc;">使用了mouseleave事件↓</div>
<div id="list2" style="display: none; position: absolute; background:#fff; border: 1px solid #ccc; width: 298px;">
<div>第一行</div>
<div>第二行</div>
<div>第三行</div>
</div>
</div>
<script type='text/javascript'>
jQuery(document).ready(function($) {
$("#title2").mouseover(function() {
var offset = $(this).offset();
$("#list2").css({left: offset.left, top: offset.top+19}).show();
});
// 绑定mouseleave事件,效果很好
$("#container2").mouseleave(function() {
$("#list2").hide();
});
});
</script>
<p>
第一行第二行第三行mouseleave和mouseout事件各有用途,因为事件冒泡在某些时候是非常有用的
解决div mouseout事件冒泡的问题
解决的办法是,使用jquery的bind方法
如:现在有一个div对象需要监听他的鼠标事件
<div class="dpx2"><div class="dpx2_px" style="cursor:pointer;" id="searchSort">请选择排序方式↓</div>
<div class="dpx2_px_xl" id="sortList" style="display:none;position:absolute;z-index:5;">
<div><a class="sortA">按时间升序↑</a></div>
<div><a class="sortA">按时间降序↓</a></div>
<div><a class="sortA">按评论数量升序↑</a></div>
<div><a class="sortA">按评论数量降序↓</a></div>
<div><a class="sortA">按点击数升序↑</a></div>
<div><a class="sortA">按点击数降序↓</a></div>
</div>
</div>
当鼠标移动到ID为searchSort的Div上时,显示下面的div。当鼠标移出下面的div时,隐藏div
JS为:
$(function(){
var sortList = $("#sortList");
$("#searchSort").mouseover(function() {
var offset = $(this).offset();
sortList.css("left", offset.left);
sortList.css("top", offset.top+20);
sortList.show();
});
//关键的一句,绑定Div对象的mouseleave事件
sortList.bind("mouseleave", function() {
$(this).hide();
});
});
本文来源:http://www.php100.com/html/program/jquery/2013/0905/5891.html
jQuery中mouseleave和mouseout的区别详解的更多相关文章
- Jquery中attr与prop的区别详解
prop()函数的结果: 1.如果有相应的属性,返回指定属性值. 2.如果没有相应的属性,返回值是空字符串. attr()函数的结果: 1.如果有相应的属性,返回指定属性值. 2.如果没有相应的属性, ...
- 基于python中staticmethod和classmethod的区别(详解)
例子 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 class A(object): def foo(self,x): print "executing foo ...
- DOS批处理中%cd%与%~dp0的区别详解
转载:https://www.jb51.net/article/105325.htm DOS批处理中%cd%与%~dp0的区别详解 Windows下批处理中%cd%和%~dp0都能用来表示当前 ...
- jquery 之height(),innerHeight(),outerHeight()方法区别详解
在jQuery中,获取元素高度的函数有3个,它们分别是height(). innerHeight().outerHeight(). 与此相对应的是,获取元素宽度的函数也有3个,它们分别是width() ...
- Jquery中find与each方法使用详解
本文实例讲述了jQuery中find与each方法用法.分享给大家供大家参考.具体如下: 一.find()方法 jquery选择器非常强大,利用css的命名规约,可以更快更方便的找出想要的元素. 图解 ...
- JavaScript 中 Property 和 Attribute 的区别详解
property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴. property ...
- jquery 中 (function( window, undefined ) {})(window)写法详解(转)
最常见的闭包 (Closure) 范式大家都很熟悉了: 123 (function() {// ...})(); 很简单,大家都在用.但是,我们需要了解更多.首先,闭包是一个匿名函数 (Anonymo ...
- C#中struct和class的区别详解
本文详细分析了C#中struct和class的区别,对于C#初学者来说是有必要加以了解并掌握的. 简单来说,struct是值类型,创建一个struct类型的实例被分配在栈上.class是引用类型,创建 ...
- Java中的==和equals的区别详解
1.基础知识 (1)String x = "hello"; (2)String x = new String ("hello"); 第1种方式的工作机制是,首先 ...
随机推荐
- Git拉取项目时报错“remote: HTTP Basic: Access denied”解决方法
问题: Git拉取项目时报错“remote: HTTP Basic: Access denied”,此问题多为本地密码与远端密码不符导致. 解决方法: 在下载地址中加上用户名和密码即可,如下: htt ...
- Java多线程之单例模式(线程安全)
package org.study2.javabase.ThreadsDemo.sync; /** * @Auther:GongXingRui * @Date:2018/9/20 * @Descrip ...
- Logging - MVC Using Log4net Save to File and Database
第一步:创建Config文件夹和log4net.config 第二步:在log4net.confg黏贴以下配置 <?xml version="1.0" encoding=&q ...
- codeforces493B
Vasya and Wrestling CodeForces - 493B Vasya has become interested in wrestling. In wrestling wrestle ...
- 扫描某目录下的所有文件的MD5码并导出文件【可执行jar】
pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http:// ...
- js自动运行
叹号后面跟函数!function 和加号后面跟函数+function 都是跟(function(){})();这个函数是一个意思,都是告诉浏览器自动运行这个匿名函数的
- Eclipse 安装Activiti插件(BPMN打开工具)
在Eclipse的菜单中打开help -> install new software: 单击add: Name: Activiti BPMN 2.0 designer Location: htt ...
- JVM深入理解<二>
以下内容来自: http://www.jianshu.com/p/ac7760655d9d JVM相关知识详解 一.Java虚拟机指令集 Java虚拟机指令由一个字节长度的.代表某种特定含义的操作码( ...
- BZOJ1005 HNOI2008明明的烦恼(prufer+高精度)
每个点的度数=prufer序列中的出现次数+1,所以即每次选一些位置放上某个点,答案即一堆组合数相乘.记一下每个因子的贡献分解一下质因数高精度乘起来即可. #include<iostream&g ...
- Phone List HDU - 1671 字典树
题意:给出一堆一组一组的数字 判断有没有哪一个是另外一个的前缀 思路:字典树 插入的同时进行判断 不过 当处理一组数字的时候 需要考虑的有两点1.是否包含了其他的序列2.是否被其他序列包含 刚开始 ...