jQuery .each 在不同的浏览器上给出不同的结果,但没有错误。它计算的元素是错误的,但为什么呢?

发布于 2024-10-03 05:44:54 字数 620 浏览 4 评论 0原文

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

这是 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);

,它会在不同的浏览器中提醒不同的数字
在 Firefox“15”中
在 IE8 中“3”
在 Chrome“6”中
在歌剧《3》中 在 Safari 中“12”

正确答案当然是“3”。无法弄清楚为什么会发生这种情况。

I need to add the one attribute of all of a certain element to an array:

Here is the 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">

Here is the jQuery

var theImages = new Array();

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

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

});

alert(theImages.length);

this alerts different numbers in different browsers
in Firefox "15"
in IE8 "3"
in Chrome "6"
in Opera "3"
in Safari "12"

The right answer is of course "3". Can't figure out why this is happening.

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

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

发布评论

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

评论(1

枕头说它不想醒 2024-10-10 05:44:54

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

Well on a quick test of this I get 3 in Safari 5, but it could be because different browsers will attempt to correct the unclosed anchor tags in different ways (assuming that wasn't a paste error). Each anchor should should have a </a> at the end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文