我有一个div:
<div id="message">
<p>Message: </p>
<p><%= message.value %></p>
<p>User: </p>
<p><%= message.user %></p>
</div>
以及具有ActionCable功能的coffescript
App.notification = App.cable.subscriptions.create "MessageChannel",
received: (data) ->
$('#messages').prepend ?
我想将数据值(data.message和data.user)传递给div,并将其添加到带有“messages”id的div。有办法吗?
THX
答案 0 :(得分:1)
<div id="messages"></div>
<div id="message">
<p>Message: </p>
<p id="pMessage"></p>
<p>User: </p>
<p id="pUser"></p>
</div>
function addMessage(data){
$("#pMessage").text(data.message);
$("#pUser").text(data.user);
$("#message").appendTo($("#messages"));
}