Table of Contents
回复内容:
Home Backend Development PHP Tutorial 请问下segmentfault中user settings页面右边的li在点击刷新后是怎么记住的?

请问下segmentfault中user settings页面右边的li在点击刷新后是怎么记住的?

Jun 06, 2016 pm 08:49 PM
bootstrap jquery php

请问下segmentfault中user settings页面右边的li在点击刷新后是怎么记住的?

如图所示,我点击了工作教育经历后,页面刷新了,此时激活的项目变成了工作教育经历了,请问这是怎么实现的?

是后台传给前台哪个li被激活了?还是采用其他的实现方法?

回复内容:

请问下segmentfault中user settings页面右边的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>
Copy after login

首先,感谢你邀请我回答。然后你所谓的激活,其实就是给li赋上了一个class="current"属性而已,至于如何判断,这个就更简单了。

SegmentFault 这个应该是在后端判断的,这个 WordPress 等程序的主题里头就有写,想法应该是跟你那个一样,每个都判断一下是否为当前页,是则加,不是则不加。

不过最近写静态博客的主题,发现他们有这么一个思路。程序有一个extendsblock函数,可以定义父模板和复写项,比如我在base.html中这样定义了

<code><li{% block index %}{% endblock %}>你好我是首页</li>
<li{% block user %}{% endblock %}>你好我是用户页</li>
<li{% blcok article %}{% endblock %}>你好我是文章页</li>
</code>
Copy after login

然后我在用户页中这么写

<code>{% extends 'base.html' %} {# 表示把base.html继承了过来 #}

{% block user %} class="current"{% endblock %} {#表示给上面定义的一个可以复写项赋值#}
</code>
Copy after login

那么最终我的用户页就会呈现这样的效果

<code><li>你好我是首页</li>
<li class="current">你好我是用户页</li>
<li>你好我是文章页</li>
</code>
Copy after login

这样也能达到目的。当然你也可以选择在前端判断,给每个li增加一个对应的class,最好和URL的调用名称一致,然后根据URL的值直接赋值给对应的对象就成了。

总的来说就是,如果你不想给<li>增加单独的标识的话,就只能后台/前端一个一个判断咯,如果增加了单独标识的话就可以快速一点,后端/前端都不局限就是。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

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

How to do vertical centering of bootstrap How to do vertical centering of bootstrap Apr 07, 2025 pm 03:21 PM

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.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

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-

How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) Apr 08, 2025 am 12:03 AM

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.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

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.

How to upload files on bootstrap How to upload files on bootstrap Apr 07, 2025 pm 01:09 PM

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).

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

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.

How to set the bootstrap navigation bar How to set the bootstrap navigation bar Apr 07, 2025 pm 01:51 PM

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)

See all articles