请问下segmentfault中user settings页面右边的li在点击刷新后是怎么记住的?
如图所示,我点击了工作教育经历后,页面刷新了,此时激活的项目变成了工作教育经历了,请问这是怎么实现的?
是后台传给前台哪个li被激活了?还是采用其他的实现方法?
回复内容:
如图所示,我点击了工作教育经历后,页面刷新了,此时激活的项目变成了工作教育经历了,请问这是怎么实现的?
是后台传给前台哪个li被激活了?还是采用其他的实现方法?
根据 URL 传入参数 $tab
判断当前页
<li<?php if ('aaa' == $tab): ?> class="current"<?php endif; ?>>xxx</li> <li<?php if ('bbb' == $tab): ?> class="current"<?php endif; ?>>xxx</li>
首先,感谢你邀请我回答。然后你所谓的激活,其实就是给li
赋上了一个class="current"
属性而已,至于如何判断,这个就更简单了。
SegmentFault 这个应该是在后端判断的,这个 WordPress 等程序的主题里头就有写,想法应该是跟你那个一样,每个都判断一下是否为当前页,是则加,不是则不加。
不过最近写静态博客的主题,发现他们有这么一个思路。程序有一个extends
和block
函数,可以定义父模板和复写项,比如我在base.html
中这样定义了
<code><li{% block index %}{% endblock %}>你好我是首页</li> <li{% block user %}{% endblock %}>你好我是用户页</li> <li{% blcok article %}{% endblock %}>你好我是文章页</li> </code>
然后我在用户页中这么写
<code>{% extends 'base.html' %} {# 表示把base.html继承了过来 #} {% block user %} class="current"{% endblock %} {#表示给上面定义的一个可以复写项赋值#} </code>
那么最终我的用户页就会呈现这样的效果
<code><li>你好我是首页</li> <li class="current">你好我是用户页</li> <li>你好我是文章页</li> </code>
这样也能达到目的。当然你也可以选择在前端判断,给每个li
增加一个对应的class
,最好和URL
的调用名称一致,然后根据URL
的值直接赋值给对应的对象就成了。
总的来说就是,如果你不想给<li>
增加单独的标识的话,就只能后台/前端一个一个判断咯,如果增加了单独标识的话就可以快速一点,后端/前端都不局限就是。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

Use Bootstrap to implement vertical centering: flexbox method: Use the d-flex, justify-content-center, and align-items-center classes to place elements in the flexbox container. align-items-center class method: For browsers that do not support flexbox, use the align-items-center class, provided that the parent element has a defined height.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

In PHP, the final keyword is used to prevent classes from being inherited and methods being overwritten. 1) When marking the class as final, the class cannot be inherited. 2) When marking the method as final, the method cannot be rewritten by the subclass. Using final keywords ensures the stability and security of your code.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

The file upload function can be implemented through Bootstrap. The steps are as follows: introduce Bootstrap CSS and JavaScript files; create file input fields; create file upload buttons; handle file uploads (using FormData to collect data and then send to the server); custom style (optional).

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

Bootstrap provides a simple guide to setting up navigation bars: Introducing the Bootstrap library to create navigation bar containers Add brand identity Create navigation links Add other elements (optional) Adjust styles (optional)
