【发布时间】:2012-09-28 20:52:28
【问题描述】:
我有一些用 json 字符串中的数据创建的 div。其中大部分是图像。在那些动态加载的图像上,并希望在鼠标悬停时显示 div 并在鼠标移出时隐藏。因此我使用了直播功能,在论坛上找到了它。 鼠标悬停功能有效,但不会鼠标移出。因此,当我将鼠标悬停在一张图像上时,会显示 div,但是当我将鼠标移出时,div 仍然可见。对此有何建议?
我的代码
<script type="text/javascript">
$('.product').live("hover", function() {
$(this).find('.product').stop(false,true); // get the image where hovering
$(this).find('div.caption').stop(false,true).fadeIn(100); // and fade in
},
function() {
$(this).find('.product').stop(false,true); // on mouse leave hide it
$(this).find('div.caption').stop(false,true).fadeOut(100); // fade out
});
</script>
更新答案 感谢下面的帮助,我找到了解决方案。
$(".productsGrid .product").live("mouseenter", function() {$(this).find('.product').stop(false,true);
$(this).find('div.caption').stop(false,true).fadeIn(100);})
$(".productsGrid .product").live("mouseleave", function() { $(this).find('.product').stop(false,true);
$(this).find('div.caption').stop(false,true).fadeOut(100);});
【问题讨论】:
-
尝试使用 mouseenter 代替 hover
-
@KaiQing - .hover() 绑定 mouseenter 和 mouseleave 事件的处理程序。
-
如果您使用的是 jquery 1.7.2 或更高版本,您应该切换到 on() 我在使用 1.7.2 时遇到了问题,因为它已被弃用。
-
你确定 $('.product') 还会在 ($(this).find('.product')) 中找到另一个 $('.product') 吗?
-
这个代码在 document.ready 中了吗?
标签: jquery