jQuery 如何在jQuery中的“this”中选择一个元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4868599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:04:43  来源:igfitidea点击:

How to select an element inside "this" in jQuery?

jquery

提问by deb

I know can I select an element this way:

我知道我可以这样选择一个元素:

$("ul.topnav > li.target").css("border", "3px double red");

but how can I do something like:

但我该怎么做:

$(this > li.target).css("border", "3px double red");

回答by hookedonwinter

$( this ).find( 'li.target' ).css("border", "3px double red");

or

或者

$( this ).children( 'li.target' ).css("border", "3px double red");

Use childrenfor immediate descendants, or findfor deeper elements.

使用children即时后代,或find进行更深入的元素。

回答by mchinta

I use this to get the Parent, similarly for child

我用它来获取父级,同样对于孩子

$( this ).children( 'li.target' ).css("border", "3px double red");

Good Luck

祝你好运