推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
python30
V2EX  ›  Python

Django 后端如何动态设置前端 a 链接的 class 为 select 或 unselect?

  •  
  •   python30 · Jun 26, 2024 · 4276 views
    This topic created in 828 days ago, the information mentioned may be changed or developed.

    问个 css 问题。如下图所示:人才信息 pkyP6e0.webp

    就是当前页面的 a 链接 css 不一样 我看了一个他的源代码: pkyPcwV.webp

    就是当首页面的 a 链接 是被选中的 class="select" 其它的链接是:class="unselect"

    我是用 django 做后端,前端 如果根据 cate.slug 来判断的话。那有多少个分类就要写多少个 if 语句了 不知道有什么更简单的功能来实现这个问题? 多谢各位

    3 replies  •  2024-07-04 20:54:25 +08:00
    luckyc
        1
    luckyc  
       Jun 26, 2024
    const allLinks = document.querySelectorAll('a');

    allLinks.forEach(link => {
    link.className = link.title === '人才信息' ? 'select' : 'unselect';
    });
    lybcyd
        2
    lybcyd  
       Jun 26, 2024
    当前页面的 active slug 可以根据路由来获取,一个判断语句就可以了。
    python30
        3
    python30  
    OP
       Jul 4, 2024
    @l4ever
    @lybcyd
    谢谢。已经解决了。就是根据当前页面的 slug 来做个判断就可以了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2291 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 08:15 · PVG 16:15 · LAX 01:15 · JFK 04:15
    ♥ Do have faith in what you're doing.