开发者

cannot access child element - jquery

开发者 https://www.devze.com 2023-01-04 17:41 出处:网络
I have a HTML like <div class=\"a\"> <div class=\"b\"> something </div> <div class=\"c\">

I have a HTML like

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

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

and jquery like

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

I Want to access the class "subC" but above is not working.

i also tried

$('.a').hover(function(){
   开发者_运维知识库 $(this).children('.c .subC').fadeOut();
})

but this too is not working !

Whats the solution to this problem ! am i doing anything wrong ? please help


children only goes one level deep. Try find() instead.

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


When inside a jQuery closure, this refers to the jQuery object returned by the previous jQuery operation:

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

Use this within the closure to set the scope for querying inside the current jQuery object:

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


Use .find('selector') to find deep children


As Rob says, use .find to find deep elements.

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

if you want to use .children, write

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

精彩评论

暂无评论...
验证码 换一张
取 消