[英]twitter bootstrap dynamically add dropdown
我正在實施一個用戶通知系統。 加載頁面后,會發出AJAX請求,並且如果存在通知,則將它們呈現為<ul>
,該<ul>
是隱藏的,但如果單擊通知項,則應顯示。
我的頁面上有幾個Bootstrap的工作下拉列表,所以這不是問題。
元素的加載和創建工作正常。 如果我檢查Firebug,它們會出現在DOM中。
// this is in the top bar
<a id="notifications" href="/user/profile/notifications" data-toggle="notifications-alert">Benachrichtigungen</a>
// and this appended to the end of body
<ul id="notifications-alert" class="notifications dropdown-menu" style="display: none;">
<li class="event_new">[..]</li>
<li class="event_new">[..]</li>
</ul>
當我追加設置data-toggle
屬性時,我也會初始化dropdown()。 像這樣:
$notifications.addAttr('data.toggle', 'notifications-alert')
.dropdown();
我也嘗試過使用手動觸發器,但是仍然可以工作。
$notifications.addAttr('data.toggle', 'notifications-alert')
.click(function(e) { e.preventDefault(); $(this).dropdown('toggle'); })
.dropdown();
任何想法為什么它不起作用?
@EDIT:我的錯誤,解決了。 有關詳細信息,請參見我的答案。
twitter bootstrap中的所有示例均顯示鏈接[tag a]和下拉列表[tag ul]以及同一父項。 也許簡單的解決方案是您應該在a之后添加ul ,而不是在主體$('a selector').after(ul)
末尾添加ul.after $('a selector').after(ul)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.