jQuery:$(this) 的直接子代

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6186523/
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 20:31:28  来源:igfitidea点击:

jQuery: Direct child of $(this)

jquery

提问by curly_brackets

Is it possible to, somehow, select a direct child of $(this)?

是否有可能以某种方式选择 $(this) 的直接子级?

I have:

我有:

var obj = $(this);  
$("ul", obj).css('width',s*w);

And need it to act like this obj > ul

并且需要它像这样 obj > ul

Is it possible?

是否可以?

回答by DanielB

$(this).children('ul')returns a list of direct children.

$(this).children('ul')返回一个直接孩子的列表。

回答by user3900346

Try the following:

请尝试以下操作:

$(this).find("> ul")

回答by inquam

The jQuery constructor can take a second parameter which can is used to override the context of the selection.

jQuery 构造函数可以采用第二个参数,用于覆盖选择的上下文。

$("ul", this);

And if you just want the first I think you could do

如果你只想要第一个,我想你可以做到

$("ul:first", this)

回答by Vixed

This works for me ;)

这对我有用;)

$('> ul',this)

回答by Alex K

this may work too (depends on what exact situation are you in maybe)

这也可能有效(取决于您可能处于什么确切情况)

$(this).find('>*:eq(0)')