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

javascript - jQuery "undefined" prints when appending to html element - Stack Overflow

programmeradmin4浏览0评论

I'm making a jQuery MP3 player. The song structure is first generated (including the information about the song), and then the structure is appended to a div using jQuery like this:

function loadFromPlaylist(playlist) {
    var songsStructure;

    for (var i=0; i<playlist.length; i++) {
        songsStructure +=
        "<div id='song" + i + "'>" +
            "<span class='mpPlaylistArtist'>" + playlist[i].artist + "</span>" +
            "<span class='mpPlaylistSong'>" + playlist[i].song + "</span>" +
            "<span class='mpPlaylistAlbum'>" + playlist[i].album + "</span>" +
        "</div>";
    }

    $('#mpTracks').append(songsStructure);
}

This works perfectly except for one thing. When the items are displayed in the browser, a string ("undefined") is printed above the songs, like so:

<div id="mpTracks">
    "undefined"
    <div id="song0">...</div>
    <div id="song1">...</div>
</div>

Googling this problem yielded alot of related problems but that didn't help me.

Does anyone know what the problem might be?

I'm making a jQuery MP3 player. The song structure is first generated (including the information about the song), and then the structure is appended to a div using jQuery like this:

function loadFromPlaylist(playlist) {
    var songsStructure;

    for (var i=0; i<playlist.length; i++) {
        songsStructure +=
        "<div id='song" + i + "'>" +
            "<span class='mpPlaylistArtist'>" + playlist[i].artist + "</span>" +
            "<span class='mpPlaylistSong'>" + playlist[i].song + "</span>" +
            "<span class='mpPlaylistAlbum'>" + playlist[i].album + "</span>" +
        "</div>";
    }

    $('#mpTracks').append(songsStructure);
}

This works perfectly except for one thing. When the items are displayed in the browser, a string ("undefined") is printed above the songs, like so:

<div id="mpTracks">
    "undefined"
    <div id="song0">...</div>
    <div id="song1">...</div>
</div>

Googling this problem yielded alot of related problems but that didn't help me.

Does anyone know what the problem might be?

Share Improve this question asked Feb 24, 2012 at 15:19 judehalljudehall 94413 silver badges29 bronze badges 2
  • Did you tried giving a default value for the songStructure? – Ajai Commented Feb 24, 2012 at 15:22
  • @Ajai the problem has been solved. I just needed to initialize the variable :) – judehall Commented Feb 24, 2012 at 15:43
Add a comment  | 

2 Answers 2

Reset to default 22

Initialize your variable to an empty string, before using it:

var songsStructure = '';

You did not set an initial value, so it is set to undefined. According to JS rules for concatination, this undefinedis then concatenated with the strings generated by the for loop leading to your result.

You have to initialize the songsStructure variable.

Write

 function loadFromPlaylist(playlist) {
        var songsStructure="";

and your problem will be solved.

发布评论

评论列表(0)

  1. 暂无评论