简体   繁体   English

twitter bootstrap动态添加下拉列表

[英]twitter bootstrap dynamically add dropdown

I'm implementing a user notification system. 我正在实施一个用户通知系统。 When the page is loaded an AJAX request is made and if notifications exist, they are rendered into a <ul> which is hidden but should show up if the notification item is clicked. 加载页面后,会发出AJAX请求,并且如果存在通知,则将它们呈现为<ul> ,该<ul>是隐藏的,但如果单击通知项,则应显示。

I have a couple of working dropdowns with Bootstrap on my page, so that's not the problem. 我的页面上有几个Bootstrap的工作下拉列表,所以这不是问题。

The loading and creating of the elements works fine. 元素的加载和创建工作正常。 They appear in the DOM if i check with Firebug. 如果我检查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>

I also initialize the dropdown() when i append set the data-toggle attribute. 当我追加设置data-toggle属性时,我也会初始化dropdown()。 Like this: 像这样:

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

I also tried with a manual trigger but still doesent work. 我也尝试过使用手动触发器,但是仍然可以工作。

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

Any ideas why it is not working? 任何想法为什么它不起作用?

@EDIT: my mistake, solved. @EDIT:我的错误,解决了。 See my answer for details. 有关详细信息,请参见我的答案。

All examples in twitter bootstrap shows both the link [tag a] and the dropdown [tag ul] together with the same parent. twitter bootstrap中的所有示例均显示链接[tag a]和下拉列表[tag ul]以及同一父项。 Maybe the easy solution is that you should add the ul after the a instead of adding the ul in the end of the body $('a selector').after(ul) 也许简单的解决方案是您应该在a之后添加ul ,而不是在主体$('a selector').after(ul)末尾添加ul.after $('a selector').after(ul)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM