我通过AJAX获得内容,例如:
<div class="nachricht" id="1">
bla
<div class="detail" id="1" style="float: left; display: none;">hidden stuff</div>
<div class="spacer" style="clear: both;"></div>
</div>
我想.details
在悬停时显示.nachricht
所以我用:
$(".nachricht").hover(function(){
$(".detail", this).stop(true,true).delay(500).show(0);
}, function (){
$(".detail", this).stop(true,true).delay(500).hide(0);
});
为什么通过AJAX加载后它不起作用,我该如何解决?
编辑:
我从类似的函数获得内容:
function loadfile(){
$.ajax({
type: 'post',
url: 'donachrichten.php',
data: {mach: "loadfile", kontaktfile: kontaktfile},
success: function(data){
$("#chatnow").html(data);
}
});
}
您可以尝试使用以下方法:
$("#chatnow").on('mouseenter', '.nachricht', function( event ) {
$(".detail", this).stop(true,true).delay(500).show(0);
}).on('mouseleave', '.nachricht', function( event ) {
$(".detail", this).stop(true,true).delay(500).hide(0);
});
解释
由于.nachricht
页面加载时类项目不存在,因此无法将事件与不存在的元素绑定。而是在element上使用事件委托#chatnow
应该起作用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句