无法访问子元素 - jquery

发布于 2024-09-06 20:27:46 字数 578 浏览 3 评论 0原文

我有一个类似 HTML

<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();
})

,但这也不起作用!

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静赏你的温柔 2024-09-13 20:27:46

children 只深入一层。请尝试使用 find() 来代替。

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

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

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

也只是曾经 2024-09-13 20:27:46

当在 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();
})

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();
})
笑梦风尘 2024-09-13 20:27:46

使用 .find('selector') 查找深度子节点

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

薄荷梦 2024-09-13 20:27:46

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

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

如果你想使用.children,请写

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

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();
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文