用这个Jquery多个选择器

时间:2011-04-18 21:07:13

标签: javascript jquery jquery-selectors

我搜索过但找不到怎么做。我正在尝试使用comment-modbox this内的元素隐藏并显示:

$('.comment-wrapper').each(function (index) {

    $(this, '.comment-modbox').mouseover(function () {
        $('.comment-modbox').show();
    });

    $(this, '.comment-modbox').mouseout(function () {
        $('.comment-modbox').hide();
    });
});

此代码隐藏并显示所有comment-modbox,无论它们是否包含在this中。

感谢您的帮助!

答案:

$('.comment-wrapper').each(function (index) {

    $(this).mouseover(function () {
        $('.comment-modbox', this).show();
    });

    $(this).mouseout(function () {
        $('.comment-modbox', this).hide();
    });
});

1 个答案:

答案 0 :(得分:6)

试试这个(jQuery("selector", context)...

$('.comment-wrapper').each(function (index) {

    $('.comment-modbox', this).mouseover(function () {
        $(this).show();
    });

    $('.comment-modbox', this).mouseout(function () {
        $(this).hide();
    });
});

第二选择:

$('.comment-wrapper').each(function (index) {

    var wrapper = this; 

    $('.comment-modbox', wrapper)
        .mouseover(function () {
            $(this).show();
        })
        .mouseout(function () {
            $(this).hide();
        });
});