.children()对jquery返回的指定索引不起作用 [英] .children() does not work on specified index of jquery return

查看:67
本文介绍了.children()对jquery返回的指定索引不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过jquery调用返回第 n 个元素的子元素.例如:

I'm trying to get the children of the nth element returned by a jquery call. For example:

var kids = $('div')[7].children();

但是,相对于children(),我一直收到此错误:

However, I keep getting this error with respect to children():

Uncaught TypeError: object is not a function

有人可以解释为什么会这样吗?只要我不在索引元素上调用children(),它就可以正常工作.

Can someone explain why this happens? children() works fine as long as I'm not calling it on an indexed element.

推荐答案

这是因为它不再是 jQuery对象.因此,您正在对DOM元素调用jQuery方法(该方法无效).

It's because it is no longer a jQuery object after you specify an index [7]. Thus, you are calling a jQuery method on a DOM element (which doesn't work).

您可以使用 .eq()方法:

You could use the .eq() method instead:

$('div').eq(7).children();

您还可以使用:

$($('div')[7]).children();

值得指出的是,这是可行的,因为DOM元素包装在$()中-从而将其转换为jQuery对象.

It's worth pointing out that this would work because the DOM element is wrapped in $() - thus turning it into a jQuery object.

这篇关于.children()对jquery返回的指定索引不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆