简体   繁体   English

循环穿过孩子

[英]Cycle through children

Basically, is there a way to 'cycle' through child elements. 基本上,有没有办法通过子元素“循环”。

ie: 即:

<ul>
<li>some text</li>
<li>some more text</li>
<li>even more text</li>
</ul>

I want to get the text from the first <li> , perform a few operations, then move on to the next one and next one until I have gone through all of the child elements. 我想从第一个<li>获取文本,执行一些操作,然后继续下一个和下一个操作,直到我完成所有子元素。

Is this possible? 这可能吗?

Your question could use a little more explanation, but jQuery's .each() is what I think you are looking for. 您的问题可以使用更多解释,但jQuery的.each()是我认为您正在寻找的。 Not to by confused with jQuery.each , which you can use to iterate over any collection, .each iterates over all of the elements that are within the jQuery object created from your selector expression. 不要混淆jQuery.each ,你可以用它来迭代任何集合, .each迭代在你的选择器表达式创建的jQuery对象中的所有元素。 It executes a callback function for each element that is part of the jQuery object. 它为作为jQuery对象一部分的每个元素执行回调函数。 The callback function is passed the index of the element and the element itself. 回调函数传递元素的索引和元素本身。 It's important to note that the callback is fired in the context of the current DOM element, so this refers to the element, and is not a jQuery object. 重要的是要注意回调是在当前DOM元素的上下文中触发的,因此this指的是元素,而不是jQuery对象。 So you could do something like: 所以你可以这样做:

$("selectorForListElem").children().each(function(index, currentElem) {
    // processing for current child element
});

I've created a very simple example over on jsFiddle . 在jsFiddle上创建了一个非常简单的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM