2011-07-25 60 views
0

我的页面在加载页面时呈现完美效果。 (当我将鼠标悬停在它们上面时,我的行应该会突出显示)。为什么live()和load()不起作用?

当我通过jQuery加载事件加载页面时,页面完美呈现,但行悬停时不突出显示。

我很确定问题在于当负载发生时DOM没有被更新。通常情况下,通过整合现场活动可以轻松解决这个问题

虽然这不行。你可能知道它为什么不起作用吗?

<script type="text/javascript"> 
$(document).ready(function() { 

$("#ListByFranchisor").live("click", function() { 
     $("#ListResultsDiv").load("advisors/recommendationsbyfranchisors.cfm").slideDown(); 
}); 

}); 
</script> 



$(".data tr:not(.head)").mouseover(function() { 
    $(this).addClass('over'); 
}); 

$(".data tr").mouseout(function() { 
    $(this).removeClass('over'); 
}); 
+0

什么是活的代码()悬停效果 - 因为这是这不是可能工作的一部分? –

+0

“悬停”功能在哪里? – PeeHaa

+1

我是否需要将实时事件添加到mouseover/mouseout事件而不是“$(”#ListByFranchisor“)。live(”click“?) –

回答

0

您需要使用live()你要绑定的动态地添加上,增加要素的功能元素,而不是事件。

$(document).ready(function() { 
    $("#ListByFranchisor").click(function() { 
    $("#ListResultsDiv").load("advisors/recommendationsbyfranchisors.cfm").slideDown(); 
    }); 

    $(".data tr:not(.head)").live('mouseover', function() { 
    $(this).addClass('over'); 
    }); 

    $(".data tr").live('mouseout', function() { 
    $(this).removeClass('over'); 
    }); 
}); 

此外,你可能想要看看delegate()以获得更好的性能。

http://api.jquery.com/delegate/

+0

这就是我的代码看起来的样子现在它完全解决了这个问题,我也是理解为什么。谢谢!!!我将检查委托()。 –

+0

@Evik james:很高兴能够帮到你:) – PeeHaa

1
$(function(){ 
    $(".data tr:not(.head)").live({ 
      mouseenter:function() 
       { 
       $(this).addClass('over'); 
       } 
    }); 

    $(".data tr").live({ 
      mouseleave:function() 
       { 
       $(this).removeClass('over'); 
       } 
    }); 
});   

EDIT

的mouseenter和鼠标悬停之间的差别在于的mouseenter(和鼠标离开)不冒泡。这意味着如果鼠标移动到你绑定的元素内(这可能不是你想要的),你将得到一个mouseover事件,而如果鼠标进入该元素本身,你将只会得到一个mouseenter事件。有关示例,请参阅documentation

REF:Jquery help me analyze what happened: mouseover/mouseout ok but not hover/unhover

Here另一个链路

+0

这就是我需要的,只是有点晚了。mouseover和mouseenter以及mouseout和mouseleave之间是否有区别? –

0

这个怎么样?

$(".data tr:not(.head)").live('mouseover',function(){ $(this).addClass('over');}).live('mouseout',function(){$(this).removeClass('over');}); 
+0

是的,这看起来像我需要的,只是有点晚,如果你能相信它。谢谢你的帮助! –

0

使用.delegate()在容器上,你就设置

$(".data").delegate('tr:not(.head)', 'mouseover', function() { 
    $(this).addClass('over'); 
}).delegate('tr.over', 'mouseout', function() { 
    $(this).removeClass('over'); 
}); 
+1

你是代码坏了我认为 – PeeHaa

+0

我想念一个(显然 – Claudio

相关问题