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'只觸發事件一次,除非我刷新頁面......我如何附加一個事件,觸發不刷新頁面多次?謝謝
誓言,你快穆罕默德。謝謝。讓我試試 – latvian 2013-04-29 14:20:37
你是男人!:)所以,我的錯誤是將事件綁定到一個被Twitter Bootstrap關閉的元素。除了添加DOM元素,我應該重新附加事件,對嗎? ..或者你的解決方案更好,因爲你在全球範圍內只做一次。這是正確的理性? – latvian 2013-04-29 14:39:12
是的,您不需要重新綁定您的活動,活動委派會照顧到這一點。 – 2013-04-29 14:41:01