0

我有一个像

<div class="a">
    <div class="b">
        something
    </div>

    <div class="c">
        <div class="subC">
            i want to access
        </div>
    </div>
</div>

和 jquery 一样

$('.a').hover(function(){
    $(this).children('.subC').fadeOut();
})

我想访问“subC”类,但上面不起作用。

我也试过

$('.a').hover(function(){
    $(this).children('.c .subC').fadeOut();
})

但这也行不通!

这个问题有什么解决办法!我做错什么了吗?请帮忙

4

4 回答 4

5

children只深一层。试试find()吧。

http://api.jquery.com/children/

于 2010-06-27T15:29:52.573 回答
1

在 jQuery 闭包中时,this指的是之前 jQuery 操作返回的 jQuery 对象:

$('.a').hover(function() {
    // 'this' is a jQuery object containing the result of $('.a')
})

在闭包内使用this来设置当前 jQuery 对象内部的查询范围:

$('.a').hover(function() {
    $('.subC', this).fadeOut();
})
于 2010-06-27T15:59:51.557 回答
0

正如 Rob 所说,用于.find查找深层元素。

$('.a').hover(function()
    {
        $(this).find('.c .subC').fadeOut();
    });

如果你想使用.children,写

$('.a').hover(function(){
    $(this).children('.c').children('.subC').fadeOut();
})
于 2010-06-27T15:52:34.453 回答
0

用于.find('selector')寻找深度儿童

于 2010-06-27T15:27:57.117 回答