开发者

Finding next element of a specific class inside a parent jQuery

开发者 https://www.devze.com 2023-01-06 08:53 出处:网络
I have the following: <div class=\"container\"> Test 1 <div class=\"child\">Testing</div>

I have the following:

<div class="container">
Test 1
<div class="child">Testing</div>
</div>

<div class="container">
Test 2
<div class="child">Testing</div>
</div>

<div class="container">
Test 3
<div class="child">Testing</div>
</d开发者_开发知识库iv>

I wish to have the child div inside the container thats hovered over to show and hide when the mouse has left the container.

I do currently have:

        $('.container').hover(
            function () {
                $(this).next('.child').show();
            },
            function () {
                $(this).next('.child').hide();
            }
        );

However it does not seem to work. Any advice appreciated, thanks.


next() is for siblings, you should use children for children :)...

 $('.container').hover(
        function () {
            $(this).children('.child').show();
        },
        function () {
            $(this).children('.child').hide();
        }
    );


use find() instead of next as it is a child element and not a sibling one:

    $('.container').hover(
        function () {
            $(this).find('.child').show();
        },
        function () {
            $(this).find('.child').hide();
        }
    );
0

精彩评论

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

关注公众号