2013-04-29 41 views
0

应用程序通过socket.io与我们的Node.js服务器进行通信。在客户端,有高音引导信息部分如下:如何为一个页面应用程序多次触发事件

<div id="message-section"> 
    <div class="alert alert-block"> 
     <button type="button" class="close" data-dismiss="alert">×</button> 
     <span>here comes message received from server and attached via Jquery .append()</span> 
    </div> 
</div> 

所以,当用户点击关闭此消息,Twitter的引导JS库中删除的整段:

<div class="alert alert-block"> 
      <button type="button" class="close" data-dismiss="alert">×</button> 
      <span>here comes message received from server and attached via Jquery .append()</span> 
     </div> 

下一页消息出现,我不能使用$('。alert')。append()来附加新消息,因为DOM元素不再存在。为了解决这个我在Twitter的引导事件正在挂钩“封闭”如下:

$('.alert').bind('closed', function() { 
     $('#message-section').append('<div class="alert alert-block"><button type="button" class="close" data-dismiss="alert">×</button></div>'); 

    }); 

有了这个,当用户关闭的消息,这将放回必需的DOM元素附加的留言。它仅适用于一次迭代。问题是事件'封闭'绑定到'.alert'只触发事件一次,除非我刷新页面......我如何附加一个事件,触发不刷新页面多次?谢谢

回答

2

尝试使用事件代表团:

$(document).on('closed','.alert', function() { 
    $('#message-section').append('<div class="alert alert-block"><button type="button" class="close" data-dismiss="alert">×</button></div>'); 
}); 

http://api.jquery.com/on/

+0

誓言,你快穆罕默德。谢谢。让我试试 – latvian 2013-04-29 14:20:37

+0

你是男人!:)所以,我的错误是将事件绑定到一个被Twitter Bootstrap关闭的元素。除了添加DOM元素,我应该重新附加事件,对吗? ..或者你的解决方案更好,因为你在全球范围内只做一次。这是正确的理性? – latvian 2013-04-29 14:39:12

+0

是的,您不需要重新绑定您的活动,活动委派会照顾到这一点。 – 2013-04-29 14:41:01

相关问题