最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery each() not working - Stack Overflow

programmeradmin1浏览0评论

Am I doing something wrong here? I have an array of tags, and when I do a jQuery each() on the array it doesn't go into the each() I did have an alert in the each but nothing happens. I have checked my error log console and there are no errors. So, what am I doing wrong?

var tags = new Array();
tags["video-games"] = "Video Games";
tags["sports"] = "Sports";
tags["movies"] = "Movies";
tags["board-games"] = "Board Games";
tags["news"] = "News";
tags["television"] = "Television";
tags["puters"] = "Computers";
tags["opinions"] = "Opinions";
tags["reviews"] = "Reviews";

function updateTags(){
    console.log(tags);
    $("div.tags > div > span:first-child").nextAll().remove();
    $.each(tags, function(key, val){
        $("div.tags > div").append("<span><a class='tag' href='/tags/" + key + "'>" + val + "</a></span>");

    });
}

updateTags();

Am I doing something wrong here? I have an array of tags, and when I do a jQuery each() on the array it doesn't go into the each() I did have an alert in the each but nothing happens. I have checked my error log console and there are no errors. So, what am I doing wrong?

var tags = new Array();
tags["video-games"] = "Video Games";
tags["sports"] = "Sports";
tags["movies"] = "Movies";
tags["board-games"] = "Board Games";
tags["news"] = "News";
tags["television"] = "Television";
tags["puters"] = "Computers";
tags["opinions"] = "Opinions";
tags["reviews"] = "Reviews";

function updateTags(){
    console.log(tags);
    $("div.tags > div > span:first-child").nextAll().remove();
    $.each(tags, function(key, val){
        $("div.tags > div").append("<span><a class='tag' href='/tags/" + key + "'>" + val + "</a></span>");

    });
}

updateTags();
Share Improve this question asked Oct 8, 2013 at 15:12 Get Off My LawnGet Off My Lawn 36.4k46 gold badges197 silver badges374 bronze badges 3
  • 2 Arrays don't work like that, use an object. – rlemon Commented Oct 8, 2013 at 15:15
  • String keys are not supported javascript array – u_mulder Commented Oct 8, 2013 at 15:15
  • You used Array[] like Object {}. – Murali Murugesan Commented Oct 8, 2013 at 15:19
Add a ment  | 

3 Answers 3

Reset to default 10

Arrays are expected to have numeric indexes.
You've created an empty array which happens to have some properties.

You should create an ordinary object instead:

var tags = {
    "video-games": "Video Games",
    ...
};

So in this case

var tags = {};
tags["video-games"] = "Video Games";
tags["sports"] = "Sports";
tags["movies"] = "Movies";
tags["board-games"] = "Board Games";
tags["news"] = "News";
tags["television"] = "Television";
tags["puters"] = "Computers";
tags["opinions"] = "Opinions";
tags["reviews"] = "Reviews";

function updateTags(){
    //console.log(tags);
    $("div.tags > div > span:first-child").nextAll().remove();
    for(var key in tags){
        $("div.tags > div").append("<span><a class='tag' href='/tags/" + key + "'>" + tags[key] + "</a></span>");
    }
}

updateTags();

Should work.

Your code is object and properties

var tags = {};
tags["video-games"] = "Video Games";

or

var tags = {
"video-games" : "Video Games";
};

Then

$.each(tags, function(key, val) {
    $("div.tags > div").append("<span><a class='tag' href='/tags/" + key + "'>" 
                                + val + "</a></span>");

});
发布评论

评论列表(0)

  1. 暂无评论