繁体   English   中英

在html列表的页眉之后和页脚之前插入项目

[英]Insert items after the header and before the footer in html list

我需要在页眉之后和页脚之前顺序插入一些项目。

基本html:

<ul id="comments_notifications_list">
    <li>header</li>
    <li>footer</li>
</ul>

我正在尝试此代码,但最后一项将被替换:

$.each(comments, function(idx, obj){ 
    $.each(obj, function(key, value){
        $('ul#comments_notifications_list:not(:first-child)').append('<li>stuff</li>');
    });
});

我该如何解决?

您可以使用afterinsertAfter jQuery方法。

这是您尝试做的例子:

 var comments = [1, 2, 3]; $("li:first-child").after($.map(comments, function(item){ return "<li>Some value" + item + "</li>"; })); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <ul id="comments_notifications_list"> <li>header</li> <li>footer</li> </ul> 

您可以尝试这种方式。 基本上,您通过$('ul#comments_notifications_list')。children()。eq(0)获得ul标记的第一个子代,然后li标记将插入到该子代之后。 有关insertAfter()的更多信息,请转到此链接http://api.jquery.com/insertafter/

 $.each(comments, function(idx, obj){ 
   $.each(obj, function(key, value){
    $('<li>stuff</li>').insertAfter($('ul#comments_notifications_list').children().eq(0));
});

暂无
暂无

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

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