如何从无序列表中删除列表元素?

时间:2017-03-27 09:43:10

标签: javascript html

我正在创建一个可滚动的消息日志来记录基于回合的格斗游戏的事件历史记录,但我不希望消息日志超过10条消息。但是由于某种原因,在多次调用函数后,它不会从列表中删除第0个列表元素(每当计数超过10时)。它反而将它们堆叠在列表的底部,将滚动菜单扩展到不合理的长度。我有很多问题:

  1. 我如何修复此问题/是否有其他方法可以删除列表 来自无序列表的元素......我怀疑它可能是因为我已经 将列表放在“nav”标签内..
  2. 是否有创建可滚动消息日志的替代方法?
  3. 此滚动消息日志是否有办法放置新消息         在顶部进入,以便将历史推向视线之外,如同         反对把新的entires放在列表的底部(out of         视线,除非用户向下滚动)?
  4. 谢谢!

    HTML:

    <nav>
    <ul id="battleLog"></ul>
    </nav>
    

    JS:

    function updateBattleLog(x) {
        var count = count+1;
        var node = document.createElement("LI");
        var textnode = document.createTextNode(x);
        node.appendChild(textnode);
        document.getElementById("battleLog").appendChild(node);
    
    
    
        if (count > 10) {
            var list = document.getElementById("battleLog");
            list.removeChild(list.childNodes[0]);
            count = count-1;
        }
    }
    

1 个答案:

答案 0 :(得分:0)

这是因为您的from django.contrib.auth.models import User from django.http import HttpResponseBadRequest from django.http import HttpResponseForbidden from django.views.generic import FormView from django import forms import django_excel as excel from clients.models import ClientContact class UploadFileForm(forms.Form): pass class ExportClientsMailXls(FormView): template_name = 'clients/export_email/export_email.html' form_class = UploadFileForm def get(self, request, *args, **kwargs): form_class = self.get_form_class() form = self.get_form(form_class) if request.user.is_staff: return self.render_to_response( self.get_context_data(form=form, can_submit=True,)) else: return HttpResponseForbidden() def post(self, request, *args, **kwargs): form_class = self.get_form_class() form = self.get_form(form_class) if request.user.is_staff: if form.is_valid(): emails = ClientContact.objects.all() for email in emails: return email.user.email column_name = ['contact_email'] return excel.make_response_from_array(emails, column_name, "xls", file_name="export_client_mail") else: return HttpResponseBadRequest() else: return HttpResponseForbidden() 变量是本地范围的,并且您在每次调用该函数时都会将其重新初始化为count(因此它永远不会达到1限制。

请改为尝试:

10

请参阅Fiddle