Javascript 有没有一种方法可以将悬停效果添加到AJAX加载的图像中?
是否可以向AJAX加载的内容/图像添加悬停效果(动画)?目前我正在使用这段代码来完成这项工作,但它只适用于在没有AJAX的情况下加载的图像Javascript 有没有一种方法可以将悬停效果添加到AJAX加载的图像中?,javascript,jquery,Javascript,Jquery,是否可以向AJAX加载的内容/图像添加悬停效果(动画)?目前我正在使用这段代码来完成这项工作,但它只适用于在没有AJAX的情况下加载的图像 $(".box img").each(function() { $(this).hover(function() { $(this).stop().animate({ opacity: 1.0 }, 500); }, function() { $(this
$(".box img").each(function() {
$(this).hover(function() {
$(this).stop().animate({ opacity: 1.0 }, 500);
},
function() {
$(this).stop().animate({ opacity: 0.3 }, 500);
});
});
或者说,这是一条路要走:
$('.box').delegate('img', {
mouseenter: function() {
$(this).stop().animate({ opacity: 1.0 }, 500);
},
mouseleave: function() {
$(this).stop().animate({ opacity: 0.3 }, 500);
}
});
顺便说一句,在您的原始代码中,不需要使用每个
-您可以使用$('.box img')。悬停(…)
,或者您可以这样做:
$('.box').delegate('img', {
mouseenter: function() {
$(this).stop().animate({ opacity: 1.0 }, 500);
},
mouseleave: function() {
$(this).stop().animate({ opacity: 0.3 }, 500);
}
});
顺便说一下,在您的原始代码中,不需要使用每个
-您可以使用$('.box img')。悬停(…)
使用
使用
生活总比生活好
比live更好使用jQuery的
.delegate()
而不是.live()
可以获得更好的性能
使用jQuery的
.delegate()
而不是.live()
可以获得更好的性能
哦,编辑了这么多次,我得到的代码与genesis'几乎相同:)哦,编辑了这么多次,我得到的代码与genesis'几乎相同:)为什么我应该使用.live()而不是.delegate()。其他人说这是一条路。@Matiss,我认为,你不应该。除非您的特定问题无法通过
.delegate
解决,否则我强烈建议使用.delegate
而不是.live
。查看他们的文档了解更多信息。为什么我应该使用.live()而不是.delegate()。其他人说这是一条路。@Matiss,我认为,你不应该。除非您的特定问题无法通过.delegate
解决,否则我强烈建议使用.delegate
而不是.live
。查看他们的文档以了解更多信息。
$('.box').delegate('img', {
mouseenter: function() {
$(this).stop().animate({ opacity: 1.0 }, 500);
},
mouseleave: function() {
$(this).stop().animate({ opacity: 0.3 }, 500);
}
});
$('.box').delegate('img','hover', function( event ) {
if( event.type === 'mouseenter' )
$(this).stop().animate({ opacity: 1.0 }, 500);
else
$(this).stop().animate({ opacity: 0.3 }, 500)
});