jQuery 选择器问题?

发布于 2024-09-16 07:45:59 字数 459 浏览 1 评论 0原文

我正在使用悬停功能将鼠标悬停在菜单中。我使用它的类选择了一个特定元素。要仅更改该元素而不更改该类的所有元素,我可以使用“this”。但我想更改该类元素内的 h3 标签。再次是我悬停过的元素,而不是所有具有该类名称的元素。

我尝试使用 > 'this' 之后的元素,但它不起作用。

我该怎么做?我希望我已经解释得足够好了。我希望你能理解使用代码。

$('.slide').hover(
    function(){
        $(this>'h3').animate({
            height: '100%'
        });
    },
    function(){
        $(this>'h3').animate({
            height: '25px'
        });
    }
);

所有答案均表示赞赏。 谢谢

I am using the hover function for mouse over in a menu. I have selected a particular element using it's class. To make changes to only that element and not all the elements with that class I can use 'this'. But I wanna make changes to the h3 tag inside that class element. Again the one I have hovered on and not all elements with that class name.

I tried using the > element after 'this' but it doesn't work.

How do I do that? I hope I have explained well enough. I hope you understand using the code.

$('.slide').hover(
    function(){
        $(this>'h3').animate({
            height: '100%'
        });
    },
    function(){
        $(this>'h3').animate({
            height: '25px'
        });
    }
);

All answers appreciated.
Thanks

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

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

发布评论

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

评论(6

残花月 2024-09-23 07:45:59

您使用 .find() 来获取 < this 内的 h3> 元素。

$('.slide').hover(
    function(){
        $(this).find('h3').animate({
            height: '100%'
        });
    },
    function(){
        $(this).find('h3').animate({
            height: '25px'
        });
    }
);

如果

是直接子元素,则使用 .children()

$(this).children('h3').animate({

You use .find() to get the <h3> element inside this.

$('.slide').hover(
    function(){
        $(this).find('h3').animate({
            height: '100%'
        });
    },
    function(){
        $(this).find('h3').animate({
            height: '25px'
        });
    }
);

If the <h3> is a direct child, it is a little more efficient to use .children():

$(this).children('h3').animate({
冷情 2024-09-23 07:45:59

试试这个:

$(this).children('h3').animate();

Try this:

$(this).children('h3').animate();
ゝ偶尔ゞ 2024-09-23 07:45:59

使用:

$(this).find('h3')

或:

$(this).children('h3')

Use:

$(this).find('h3')

Or:

$(this).children('h3')
倥絔 2024-09-23 07:45:59

尝试使用 $(this).find("h3") 而不是 $(this>'h3')

Try $(this).find("h3") instead of $(this>'h3').

三生池水覆流年 2024-09-23 07:45:59

您应该使用 $(this).find('h3') ,

  $('.slide').hover(
    function(){
        $(this).find('h3').animate({
            height: '100%'
        });
    },
    function(){
        $(this).find('h3').animate({
            height: '25px'
        });
    }
);

或者如果您只想将 h3:s 直接放在 .slide 下方,则可以使用

$(this).children('h3')

You should use $(this).find('h3') as in

  $('.slide').hover(
    function(){
        $(this).find('h3').animate({
            height: '100%'
        });
    },
    function(){
        $(this).find('h3').animate({
            height: '25px'
        });
    }
);

or if you only want the h3:s directly underneath .slide you can use

$(this).children('h3')
独孤求败 2024-09-23 07:45:59

以上所有答案都是正确的。选择器不起作用的原因是普通的“this”关键字指向实际的 DOM 元素。您必须将“this”单独包装在 jQuery 对象中,如下所示:$(this),然后才能对其使用任何 jQuery 方法。

All of the above answers are correct. The reason why your selector didn't work is that the plain 'this' keyword points to the actual DOM element. You have to wrap 'this' in a jQuery object by itself like so: $(this) before you can use any jQuery methods on it.

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