2011-02-18 71 views
0

$(this):first-child是错误的语法

我不知道如何在.each循环使用first-child$(this)

+1

哪来的代码? – nyuszika7h 2011-02-18 09:23:12

回答

0

这可以工作,但我不知道如果订单有保证:

$(this).children()[0]; 
0

我想“这”不是一个jQuery对象,而是一个DOM对象,就不会这样做你要?

this.childNodes[0] 

这也将返回文本节点,不知道如果JQuery做到这一点。

这就是说,如果你只想要第一个“格”子子节点与myClass类的节点,请您.each循环不同

$(".myClass > div:fist-child").each(function() {...}) 
+1

它看起来像一个jQuery对象。 `children()`不返回文本节点,但`find()`做。 – 2011-02-18 07:47:55

+1

@Andrew;他使用`.each`,其中`this`是指DOM对象(这就是为什么如果你想对它执行JQuery操作,你需要像`$(this)`一样包装它)。 – falstro 2011-02-18 09:03:29

3

你应该尝试

$(':first-child', this) 

看到这个例子on jsbin

+0

尽管你在我之前发布了它,但我的解决方案更快,因为你的电话是`.find()`。看看[jQuery源代码](https://github.com/jquery/jquery/blob/master/src/core.js#L170-174)。 – nyuszika7h 2011-02-18 08:04:28