迭代 jQuery $(this).attr('class').split(" ") 给出奇怪的结果

发布于 2024-09-08 09:48:12 字数 746 浏览 4 评论 0 原文

我有一个页面,我正在尝试为许多共享公共类的 div 获取类数组。例如:

<div class="common lorem ipsum"></div>
<div class="common dolor sit"></div>
<div class="common hello world"></div>

我想获取每个 common 类 div 并获取其类的数组。目前,我正在使用 jQuery 来完成此操作:

$('.common').each(function(index) {
  var classes = $(this).attr('class').split(" ");
  for(var i in classes) {
    alert(classes[i]);
  }
});

查看第一个生成的 classes 变量会得出以下结果:

classes: Array (3)
0: "common"
1: "lorem"
2: "ipsum"
length: 3
__proto__: Array

问题是 for(var i in classes) 似乎正在迭代 __proto__ 数组并深入研究它 - 有人以前遇到过这个吗?我使用的是最新版本的 Chrome (6.0.453.1)。

I've got a page where I'm trying to fetch arrays of classes for lots of divs which share a common class. For example:

<div class="common lorem ipsum"></div>
<div class="common dolor sit"></div>
<div class="common hello world"></div>

I want to fetch each common class div and get an Array of it's classes. At the moment, I'm doing it by using this bit of jQuery:

$('.common').each(function(index) {
  var classes = $(this).attr('class').split(" ");
  for(var i in classes) {
    alert(classes[i]);
  }
});

Looking at the first resulting classes variable gives this:

classes: Array (3)
0: "common"
1: "lorem"
2: "ipsum"
length: 3
__proto__: Array

The problem is that the for(var i in classes) seems to be iterating over the __proto__ Array and delving down into that as well - has anybody ever come across this before? I'm using the latest version of Chrome (6.0.453.1).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

帅气称霸 2024-09-15 09:48:12
for ( var i = 0, l = classes.length; i<l; ++i ) {
 alert( classes[i] );
}

使用常规 for 循环(而不是 for...in)迭代数组,否则它会枚举数组的属性(因为它仍然是一个对象,并且除了内部元素之外还具有其他属性)。

for ( var i = 0, l = classes.length; i<l; ++i ) {
 alert( classes[i] );
}

Iterate through an array with a regular for loop, not a for...in otherwise it enumerates through the array's properties ( since its still an object and has other properties in addition to the elements inside ).

很酷不放纵 2024-09-15 09:48:12

要添加到其他有效答案,由于您已经在使用 jQuery,因此可以利用 jQuery.each

$.each(classes, function (i, cls) {
    alert(cls);
});

To add to the other valid answers, since you're already using jQuery, you can take advantage of jQuery.each:

$.each(classes, function (i, cls) {
    alert(cls);
});
叹沉浮 2024-09-15 09:48:12

@meder 正确地回答了你的问题,我只是想补充一点,如果枚举的顺序不重要,你可以随时使用这种简化的形式:

for ( var i = classes.length; i--; ) {
  alert( classes[i] );
}

它更短,更快。

@meder answered your question just right, i've just wanted to add, that if the order of the enumeration is not important, you can always use this simplified form:

for ( var i = classes.length; i--; ) {
  alert( classes[i] );
}

It is shorter, and faster.

旧街凉风 2024-09-15 09:48:12

添加到 meder 的答案...

有一种方法可以安全地迭代对象,而不会被对象的继承属性所困扰。 hasOwnProperty() 来救援:

for(var i in classes) {
  if (classes.hasOwnProperty(i)) {
    var safeValue = classes[i];
  }
}

Adding to meder's answer...

There is a way of iterating over objects safely without being annoyed by the inherited properties of an object. hasOwnProperty() to the rescue:

for(var i in classes) {
  if (classes.hasOwnProperty(i)) {
    var safeValue = classes[i];
  }
}
狼性发作 2024-09-15 09:48:12

我在此处看到了这个用法。

我不确定 (i in this)0-n 的双重检查会消除什么。

var thisp = arguments[1];
for (var i = 0, len = this.length; i < len; i++){
  if (i in this){
    fun.call(thisp, this[i], i, this); // fun(element,index,array)
  }
}

I've seen this used here.

I'm not sure what the (i in this) double-check against 0-n weeds out.

var thisp = arguments[1];
for (var i = 0, len = this.length; i < len; i++){
  if (i in this){
    fun.call(thisp, this[i], i, this); // fun(element,index,array)
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文