1

我正在尝试做一些与这个问题中解释的非常相似的事情 - jQuery hover : fading in a hidden div while fade out the "default" one

我遇到的问题是我想在重复类上使用淡入/淡出,而不是这个使用单个 ID 作为选择器的示例。在将鼠标悬停在我的图像上的那一刻,页面上使用同一类的所有其他图像也会消失。这是我所拥有的:

HTML

<div class="test">
            <div class="img rounded">
                <div class="post_image"> <a href="#"><img src="http://graphics8.nytimes.com/images/2009/07/19/arts/Pool4.jpg" border="0"></a>
                </div>
            </div>
            <div class="post_body hide">
                <p>This is a dummy text to fadeThis is a dummy text to fadeThis is a dummy text to fadeThis is a dummy text to fadeThis is a dummy text to fade This is a dummy text
                </p>
            </div>
        </div>


            <div class="test">
            <div class="img rounded">
                <div class="post_image"> <a href="#"><img src="http://graphics8.nytimes.com/images/2009/07/19/arts/Pool4.jpg" border="0"></a>
                </div>
            </div>
            <div class="post_body hide">
                <p>This is a dummy text to fadeThis is a dummy text to fadeThis is a dummy text to fadeThis is a dummy text to fadeThis is a dummy text to fade This is a dummy text
                </p>
            </div>
        </div>

jQuery

<script type="text/javascript">
$(function(){
$('.test').hover(
        function(){
            $('.post_image').fadeOut(100, function(){
                $('.post_body').fadeIn(100);                         
            });
        },
        function(){
            $('.post_body').fadeOut(100, function(){
                $('.post_image').fadeIn(100);                        
            });
        }
        );


});
</script>

我很确定我在 jQuery 中需要一个 $(this) 但不能让它工作。非常感谢任何帮助!

4

1 回答 1

0

$(this)对象和函数的使用find()应该更适合您:

$(function(){
    $('.test').hover(function(){
        $(this).find('.post_image').fadeOut(100,function(){
            $('.post_body').fadeIn(100);
        });
    },function(){
        $(this).find('.post_body').fadeOut(100,function(){
            $('.post_image').fadeIn(100);
        });
    });
});
于 2012-04-12T22:36:14.573 回答