使用ajax内容在jQuery中悬停时显示/隐藏div

时间:2011-05-10 10:55:11

标签: jquery

我目前有代码显示/隐藏另一个div悬停或关闭时的div。 此代码适用于它应用于的初始批量数据:

$(document).ready(function() {
    $('.eventViewWrapper').hover( function(){
        var eventId = $(this).attr('rel');
        $("#eventActions_" + eventId).show();
   },
   function(){
      var eventId = $(this).attr('rel');
      $("#eventActions_" + eventId).hide();
   });
});

然而,当我通过ajax帖子获取更多数据时,我悬停时div不再显示。 我理解为什么会发生这种情况,因为它发生在之前的ajax功能并需要实现.live()方法。所以我做了以下更改:

$(document).ready(function() {
   $('.eventViewWrapper').live('hover', function() {
        var eventId = $(this).attr('rel');
        $("#eventActions_" + eventId).show();
   },
   function(){
      var eventId = $(this).attr('rel');
      $("#eventActions_" + eventId).hide();
   });
});

现在这部分工作,因为当我悬停时div出现。但是,当我将焦点从div中移开时,它并没有隐藏。我想,代码的第二部分也需要将.live()方法链接到它,我只是不确定如何?

由于

1 个答案:

答案 0 :(得分:1)

您可以简单地使用mouseentermouseleave事件,因为这是.hover()内部使用的事件:

$('.eventViewWrapper').live('mouseenter', function () {
    var eventId = $(this).attr('rel');
    $("#eventActions_" + eventId).show();
}).live('mouseleave', function () {
    var eventId = $(this).attr('rel');
    $("#eventActions_" + eventId).hide();
});