【问题标题】:jQuery .each is giving different results on different browsers but no errors. It counts the elements wrong, but why?jQuery .each 在不同的浏览器上给出不同的结果,但没有错误。它计算元素错误,但为什么呢?
【发布时间】:2010-11-18 01:37:50
【问题描述】:

我需要将某个元素的所有属性添加到数组中:

这里是html:

<a class="umb" name="/wordpress/images/custom-slideshow/image1.jpg">  
<a class="umb" name="/wordpress/images/custom-slideshow/image2.jpg">  
<a class="umb" name="/wordpress/images/custom-slideshow/image3.jpg">

这里是 jQuery

var theImages = new Array();

$('.umb').each(function(){

    theImages.push($(this).attr('name'));

});

alert(theImages.length);

这会在不同的浏览器中提醒不同的号码
在火狐“15”
在 IE8“3”
在 Chrome“6”中
在歌剧“3”中 在 Safari“12”中

正确答案当然是“3”。无法弄清楚为什么会这样。

【问题讨论】:

  • 您发布的不是有效的 HTML,您的实际 HTML 是什么样的?
  • 一方面,您是否注意到它们都是 3 的倍数?这引出了一个问题,代码何时执行

标签: jquery arrays cross-browser push each


【解决方案1】:

在快速测试中,我在 Safari 5 中得到 3,但这可能是因为不同的浏览器会尝试以不同的方式更正未闭合的锚标记(假设这不是粘贴错误)。每个锚点的末尾都应该有一个。

【讨论】:

  • 我还是想知道为什么浏览器的计算方式不同。
猜你喜欢
  • 1970-01-01
  • 2013-05-01
  • 2013-08-30
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多