2017-08-02 41 views
-1

我正在使用node.js和express.I想要從div標籤中的socket.io傳遞接收到的數據。根據從socket.io收到的數據,我想檢查下面的for和if語句。將接收到的數據傳遞給使用JavaScript的Express代碼

 - for(var i in rows)  
     -if (rows[i]['senderID'] == "1" && rows[i]['receiverID'] == "2") 
      div.direct-chat-msg 
       div.direct-chat-name.pull-left 
       div.direct-chat-info.clearfix 
        span.direct-chat-name.pull-left #{rows[i]['senderID']} 
        span.direct-chat-timestamp.pull-right #{rows[i]['sentDate']} 
       img.direct-chat-img(src="/images/users/avatar04.png", alt="alt") 
       div.direct-chat-text #{rows[i]['message']} 
     -else if (rows[i]['senderID'] == "2" && rows[i]['receiverID'] == "1") 
       div.direct-chat-msg.right 
       div.direct-chat-name.pull-right 
        div.direct-chat-info.clearfix 
        span.direct-chat-name.pull-left #{rows[i]['senderID']} 
        span.direct-chat-timestamp.pull-right #{rows[i]['sentDate']} 
        img.direct-chat-img(src="/images/users/avatar.png", alt="alt") 
        div.direct-chat-text #{rows[i]['message']}  
     -else 
       div.direct-chat-msg.right 
       p No message! 
    div.box-footer 
     div.input-group 
     input.form-control(type="text" id="message" placeholder="Write a message...") 
     span.input-group-btn 
      button.btn.btn-danger.btn-flat(type="submit" onclick="sendMessageJS()") Send 

哪能下面的HTML標籤下運行該代碼:

div.direct-chat-messages(id="chatArea") 

回答

0

我解決了這個問題,增加新的HTML變量。例如

var content = '<div class = "direct-chat-msg">' + 
         '<div class = "direct-chat-name pull-left">' + 
          '<div class = "direct-chat-info clearfix">' + 
           '<span class = "direct-chat-name pull-left">' + data[i]["senderID"] + '</span>' + 
           '<span class = "direct-chat-timestamp pull-right">' + data[i]["sentDate"] + '</span>' + 
          '</div>' + 
          '<img class = "direct-chat-img" src= "/images/users/avatar04.png" alt = "resim">' + 
          '<div class= "direct-chat-text">'+ data[i]["message"] +'</div>' + 
         '</div>' + 
         '</div>'; 
var chatArea = document.getElementById("chatArea"); 
chatArea.innerHTML += content; 
相關問題