SyntaxError:missing;在声明之前.each

时间:2014-10-19 17:05:34

标签: javascript jquery each

function readMessages(data, textStatus) 
{
    clearChat = data.clear;

    if (clearChat == 'true') {
        $('#chatmessageinner')[0].innerHTML = "";
        lastMessageID = -1;
    }

    if (data.messages.length > 0)
    {
        if(lastMessageID > data.messages[0].id) return;
        lastMessageID = data.messages[data.messages.length - 1].id;
    }

    $.each(data.messages, function(i, message) {
        var htmlMessage = "";
        htmlMessage += "<div class="item" style="color:" + message.color + "">";
        htmlMessage += "[" + message.time + "] " + message.name + " said: <br/>";
        htmlMessage += message.message;
        htmlMessage += "</div>";

        $('#chatmessageinner')[0].innerHTML += htmlMessage;
    });

}

这是功能,错误是:

SyntaxError: missing ; before statement
In line $.each(data.messages, function(i, message) { 

有什么问题?请帮帮我。

3 个答案:

答案 0 :(得分:1)

我认为你的问题是这一行

htmlMessage += "<div class="item" style="color:" + message.color + "">";

您在双引号内使用双引号,应该像

htmlMessage += "<div class=\"item\" style=\"color:" + message.color + "\">";

答案 1 :(得分:1)

你必须逃避你的字符串:

htmlMessage += "<div class="item" style="color:" + message.color + "">";

这是错的,应该是:

htmlMessage += "<div class=\"item\" style=\"color:" + message.color + "\">";

或者,更好,请使用单引号:

htmlMessage += '<div class="item" style="color:' + message.color + '">';

答案 2 :(得分:1)

构建字符串的代码有点过时了。

$.each(data.messages, function(i, message) {
    var htmlMessage = "";
    htmlMessage += "<div class='item' style='color:" + message.color + "'>";
    htmlMessage += "[" + message.time + "] " + message.name + " said: <br/>";
    htmlMessage += message.message;
    htmlMessage += "</div>";

    $('#chatmessageinner')[0].innerHTML += htmlMessage;
});