Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有没有一种方法可以将悬停效果添加到AJAX加载的图像中?_Javascript_Jquery - Fatal编程技术网

Javascript 有没有一种方法可以将悬停效果添加到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

是否可以向AJAX加载的内容/图像添加悬停效果(动画)?目前我正在使用这段代码来完成这项工作,但它只适用于在没有AJAX的情况下加载的图像

    $(".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)
});