我正在通过推送器将事件推送到以下代码: 我没有从alertDivNotification获得预期的html。
<!-- output -->
<div class="alert alert-danger alert-server" role="alert">Hello</div>
<!-- expected -->
<div class="alert alert-danger alert-server" role="alert">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Moving Servers</strong> Hello
</div>
var channel = pusher.subscribe('my-channel');
channel.bind('my-event', function(data) {
var alertDivNotification = $('<div class="alert alert-danger alert-server" role="alert"><button type="button" class="close" data-dismiss="alert">×</button><strong>Moving Servers</strong> message</div>');
alertDivNotification.html(data.message);
$('#alertDivContainer').prepend(alertDivNotification);
});
答案 0 :(得分:1)
问题在于:
var alertDivNotification = $('<div class="alert alert-danger alert-server" role="alert"><button type="button" class="close" data-dismiss="alert">×</button><strong>Moving Servers</strong> message</div>');
alertDivNotification.html(data.message);
我假设您希望Pusher事件中的data.message
替换HTML中的文字“message”。但jQuery .html
方法正在替换<div>
的所有子项,包括您的按钮。而是使用.append
,附加从data.message
构造的文本节点。像这样:
var alertDivNotification = $('<div class="alert alert-danger alert-server" role="alert"><button type="button" class="close" data-dismiss="alert">×</button><strong>Moving Servers</strong></div>');
alertDivNotification.append(document.createTextNode(data.message));
(编辑)如果您想在Pusher事件中添加data.notification_title
并显示该事件,您可以:
var alertDivNotification = $('<div class="alert alert-danger alert-server" role="alert"><button type="button" class="close" data-dismiss="alert">×</button></div>');
alertDivNotification.append($('<strong></strong>').append(document.createTextNode(data.notification_title)));
alertDivNotification.append(document.createTextNode(data.message));