【发布时间】:2017-11-20 11:33:41
【问题描述】:
我正在使用django-el-pagination,django package that allows ajax pagination。我正在对Comment(cmets 列表)的查询集进行分页。查询集在comments.html 内部,在comments_base.html 内部,在article.html 内部(父视图)。以下是我的看法:
def article(request, category, id, extra_context=None):
name = resolve(request.path).kwargs['category']
instance = get_object_or_404(Post, id=id, entered_category=name)
new_comments_list = Comment.objects.filter(destination=id, parent_id=0).order_by('-timestamp')
template = 'article.html'
page_template = 'comments.html'
if request.is_ajax():
template = page_template
context = {
'id': id,
'comment_list': new_comments_list,
'page_template': page_template,
'instance': instance,
}
if extra_context is not None:
context.update(extra_context)
return render(request, template, context)
cmets_base.html
{% block comments_base %}
<div class="commentsContainer">
<div class="endless_page_template">
{% include 'comments.html' %}
</div>
{% block js %}
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="{% static 'js/el-pagination/js/el-pagination.js' %}"></script>
<script>
$.endlessPaginate({
});
</script>
{% endblock %}
</div>
{% endblock %}
cmets.html
{% block comments %}
{% paginate 10 comment_list %}
{% for i in comment_list %}
{% if i.parent_comment %}
<div class="comment_shell hasParent">
{% else %}
<div>
{% endif %}
<div class='comment_div' data-comment_id="{{ i.id }}">
<div class="left_comment_div">
<div class="username_and_votes">
<h3><a class='username_foreign'>{{ i.user }}</a></h3>
{% for j in i.score.all %}
<span class="upvotes">{{ j.upvotes }}</span>
<span class="downvotes">{{ j.downvotes }}</span>
{% endfor %}
</div>
<br>
<p>{{ i.comment_text }}</p>
</div>
</div>
{% include 'comments.html' with comment_list=i.replies.all %}
</div>
{% endfor %}
{% show_pages %}
{% endblock %}
所以当我转到分页中的下一组 cmets 时,jQuery 不起作用。我认为这是因为它被附加或动态内容。所以我使用了on()方法which other answers say to do。但是它仍然不起作用。我将仅展示一个简单的示例来说明它不起作用:
$('.upvotes').on('click', function() {
$(this).css('color', '#fff');
});
点击时不改变颜色。那么它仍然无法正常工作有什么原因,我该如何解决?
【问题讨论】:
-
你为什么使用 jquery。请改用
(click)。 -
我试过了,还是不行。加上其他所有答案都说要使用
on():stackoverflow.com/questions/20962471/… 编辑:点击是指$('.upvotes').click(function()? -
您想要达到的目标是什么。告诉我,我会帮助你解决这个问题。
-
jQuery 不适用于附加(动态)内容。所以 jQuery 不适用于我在第一个分页之后的每个评论集。
标签: javascript jquery ajax django pagination