根据URL Jquery Django将活动类添加到元素

时间:2015-08-05 09:54:39

标签: jquery css django

我如何添加"活跃"根据href

对元素进行分类

我有一个像这样的django模板:

<div class="panel side-panel-1 category">
<h4 class="title1">Разделы</h4>
<ul class="clear-list">
{% for section in sections %}
    <li>
        <a href="/advert/adverts.aspx&sec{{ section.id }}">{{ section.name }}  ({{ section.advert_set.count }})</a>
    </li>
{% endfor %}
</ul>

所以网址将由section.id

组成

实施例

  • mysite.com/advert/adverts.aspx&sec1

在某些情况下可能是:

  • mysite.com/advert/adverts.aspx&sec1&cat10&page1

2 个答案:

答案 0 :(得分:0)

也许你想这样做。仅使用class="{{ section.id ? 'active' : '' }}"。如果section.id存在,则添加活动的wile。

<div class="panel side-panel-1 category">
<h4 class="title1">Разделы</h4>
<ul class="clear-list">
{% for section in sections %}
    <li class="{{ section.id ? 'active' : '' }}" >
        <a href="/advert/adverts.aspx&sec{{ section.id }}">{{     section.name }}  ({{ section.advert_set.count }})</a>
   </li>
{% endfor %}
</ul>

答案 1 :(得分:0)

在views.py中

def adverts_sec(request, advert_sec):
args = {}
args.update(csrf(request))
args['adverts'] =  Advert.objects.filter(advert_section_id=advert_sec).order_by('-advert_date', '-id')
args['sections'] = AdvertSection.objects.all().order_by('-name')
args['categs'] = AdvertCategory.objects.all().order_by('-name')
args['current_section'] = AdvertSection.objects.get(id=advert_sec)
args['username'] = auth.get_user(request).username
args['reg_form'] = MyRegistrationForm()
return render_to_response('adverts_sec.html', args)

然后在模板中:

{% for section in sections %}
    <li class="{% if section.id == current_section.id %}
                    active
                {% else %}
                {% endif %}">
        <a href="/advert/adverts.aspx&sec{{ section.id }}">{{ section.name }}  ({{ section.advert_set.count }})</a>
    </li>
{% endfor %}

所以,如果我们有&#34; current_section&#34;然后django将它与{{section.id}}进行比较,匹配的那个将获得一个“活跃的”#39;类

感谢所有帮助过我的人