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