为了..参加加时赛但仅限于 IE8?

发布于 2024-12-01 09:26:37 字数 992 浏览 1 评论 0原文

我尝试过自己解决这个问题,但我没有想法。

我有一个循环,仅通过循环数组来创建列表项。但它似乎每次都会执行额外的循环,因此会拉出未定义的元素。例如,我的数组停在 albums[0] 处,但它会继续尝试对不存在的 albums[1] 执行此操作。奇怪的是,更具体地说,它最终从我的过滤功能中提取文本?

为什么在 chrome 中不会发生这种情况? 这就是 ie8 中发生的情况 http://dl.dropbox.com/u/1261672/groovyBox2222/guts/artists.html?Hackers-vs-Slackers&

 getPlaylist(function(songs) // pulls out songs matching artist
 {

    var albums = new Array();
    for (obj in songs){
        if (songs[obj]["artist"] == artist){
            albums.push(songs[obj]["album"]);
        }

        albums = removeDuplicateElement(albums);
    }
    var albumname;
    for (x in albums){ // creates LI of albums
        albumname = '<li><a href="albums.html?'+escape(albums[x])+
           '&'+escape(artist)+'">'+albums[x]+"</a></li>";
        $('#albumlist').append(albumname);
    }
};

I have tried figuring this out on my own but am out of ideas.

I have a loop that just makes list items by looping through an array. But it seems to be doing an EXTRA loop each time, therefore pulling an undefined element. e.g. my array stops at albums[0] but it goes ahead and tries to do it for albums[1] which doesn't exist. weirdness ensues, more specifically, it ends up pulling the text from my filter function??

how come this doesn't happen in chrome?
this is where it happens in ie8
http://dl.dropbox.com/u/1261672/groovyBox2222/guts/artists.html?Hackers-vs-Slackers&

 getPlaylist(function(songs) // pulls out songs matching artist
 {

    var albums = new Array();
    for (obj in songs){
        if (songs[obj]["artist"] == artist){
            albums.push(songs[obj]["album"]);
        }

        albums = removeDuplicateElement(albums);
    }
    var albumname;
    for (x in albums){ // creates LI of albums
        albumname = '<li><a href="albums.html?'+escape(albums[x])+
           '&'+escape(artist)+'">'+albums[x]+"</a></li>";
        $('#albumlist').append(albumname);
    }
};

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

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

发布评论

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

评论(2

抽个烟儿 2024-12-08 09:26:37

循环数组时切勿使用 for in...这是不可预测的。使用这个代替:

for (var i = 0; i < albums.length; i++) {
    var obj = albums[i];
}

Never use for in when looping over an array... it's unpredictable. Use this instead:

for (var i = 0; i < albums.length; i++) {
    var obj = albums[i];
}
靖瑶 2024-12-08 09:26:37

您正在使用“foreach”循环,这导致了问题。改为这样做:

for (var i = 0; i < albums.length; i++)
{
    // your code
}

You are using a 'foreach' loop, which is causing the problem. Do this instead:

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