2013-06-25 81 views
1

我正在实现用户通知系统。当页面被加载时,一个AJAX请求被做出,并且如果通知存在,它们被渲染成一个<ul>,它被隐藏,但是如果通知项目被点击则应该出现。twitter bootstrap动态添加下拉框

我在我的页面上有几个带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属性。像这样:

$notifications.addAttr('data.toggle', 'notifications-alert') 
       .dropdown(); 

我也尝试过使用手动触发器,但仍然没有工作。

$notifications.addAttr('data.toggle', 'notifications-alert') 
       .click(function(e) { e.preventDefault(); $(this).dropdown('toggle'); }) 
       .dropdown(); 

任何想法,为什么它不工作?

@EDIT:我的错误,解决了。详情请参阅我的回答。

回答

1

twitter bootstrap中的所有示例都显示了链接[tag a]和下拉[tag ul]以及相同的父级。也许最简单的办法是,你应该一个后添加UL而不是在身体的末尾添加UL$('a selector').after(ul)