简体   繁体   English

使用jQuery选择器迭代DOM元素

[英]Using jQuery selectors to iterate through DOM elements

Can someone tell me why this won't work? 有人能告诉我为什么这不起作用?

var top = 0;
for (divToPosition in $('.positionableDiv')) {
   divToPosition.css('top',top+'px');
   top = top + 30;
}

The first reason would be the misuse of the for loop. 第一个原因是滥用for循环。

jQuery has an idiom for looping over selected elements.. jQuery有一个用于循环所选元素的习惯用法。

var top = 0;
$('.positionableDiv').each(function() {
   $(this).css('top',top+'px');
   top = top + 30;
});

Please take a look at for...in for a better understanding of the way a for...in loop works in javascript, it does not enumerate in the same way as say .NET or Java. 请查看...以便更好地理解for循环在javascript中的工作方式,它不会像.NET或Java那样枚举。

From Article: 来自文章:

Although it may be tempting to use this as a way to iterate over an Array, this is a bad idea. 尽管使用它作为迭代数组的方法可能很诱人,但这是一个坏主意。

The correct way to iterate over a set of matched elements is with .each , as other answers have mentioned. 正如其他答案所提到的,迭代一组匹配元素的正确方法是.each Attempting to use a for..in loop will iterate over the properties of the jQuery object, not the elements it matched. 尝试使用for..in循环将迭代jQuery对象的属性,而不是它匹配的元素。

To improve slightly on some of the other .each examples, you could omit the top variable to clean things up a little. 要稍微改进一些其他.each示例,您可以省略top变量来清理一些东西。 The first parameter to .each is in the index of the element within the set of matched elements; .each的第一个参数位于匹配元素集合中元素的索引中; you could achieve the same thing by multiplying it by 30 at each step. 你可以通过在每一步乘以30来实现同样的目标。 There is no incrementing and no top variable cluttering things up: 没有递增,也没有top变量混乱:

$('.positionableDiv').each(function(i) {
    $(this).css('top', (i * 30) + "px");
});

This would work: 这可行:

var top = 0;
for (var pdiv in $('.positionableDiv').get()) {
   $(pdiv).css('top', top + 'px');
   top = top + 30;
}

Basically, you use get() to retrieve the array of elements. 基本上,您使用get()来检索元素数组。

The "for (var key in obj)" is suitable for iterating the members of an object. “for(obj中的var键)”适用于迭代对象的成员。 It works for native JavaScript arrays as long as their prototype is not extended. 只要原型未扩展,它就适用于本机JavaScript数组。 A jQuery object may look as a native JavaScript array but it is not hence "for ( in )" fails. jQuery对象可能看起来像是一个本机JavaScript数组,但它不是因为“for(in)”失败。

You can use .each to iterate over a jQuery object: var top = 0; 您可以使用.each迭代jQuery对象:var top = 0;

$('.positionableDiv').each(function() {
     $(this).css('top', top);
     top += 30;
});

The $('.positionableDiv') in your statement is a jQuery object with a great many properties. 您的语句中的$('。positionableDiv')是一个具有许多属性的jQuery对象。 What you want is to iterate through the matched elements which is not done that way. 你想要的是迭代匹配的元素,这是不是这样做的。

Try: 尝试:

var top = 0;
$('.positionableDiv').css('top', function(index, value) {
    var cTop = top;
    top = top + 30;
    return cTop + 'px';
});

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

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