如何动态改变网页所选菜单项的颜色?
我是开发网页的新手。我希望创建类似于 stackoverflow.com 中的菜单(如上面显示的问题、标签、用户)。如何更改所选菜单的颜色(例如,“单击”时问题的背景颜色更改为橙色)?
我已经设法在悬停时更改颜色(使用 CSS),因为这很简单,但我遇到了麻烦。
我可以仅使用 CSS 来实现更改单击项目的颜色的效果吗?
I am new to developing web pages. I am looking to create menus similar to the ones in stackoverflow.com (like Questions, Tags, Users shown above). How do I change the color of the selected menu (for example, the background color of the Question changes to orange when 'clicked')?
I have managed to change the color while hovering (using CSS) as that was simple, but I am having trouble with this.
Can I achieve this effect of changing the color of a clicked item using only CSS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
设置类活动和悬停的样式:
然后您需要在服务器端使 li 处于活动状态。
因此,当您绘制菜单时,您应该知道加载了哪个页面并将其设置为:
但是如果您在不重新加载的情况下更改内容,则无法更改服务器上的活动 li 元素,您需要使用 javascript:
Set the styles for class active and hover:
Than you need to make the li active, on the server side.
So when you are drawing the menu, you should know which page is loaded and set it to:
But if you are changing the content without reloading, you cannot change set the active li element on the server, you need to use javascript:
使用 JavaScript 实现这一点可能是最简单的...这是一个用于演示的 JQuery 脚本...正如其他人提到的...我们有一个名为“active”的类来指示活动选项卡 - 而不是伪类“:积极的。'我们可以很容易地将其命名为任何名称……选定的、当前的等等。
It would probably be easiest to implement this using JavaScript ... Here's a JQuery script to demo ... As the others mentioned ... we have a class named 'active' to indicate the active tab - NOT the pseudo-class ':active.' We could have just as easily named it anything though ... selected, current, etc., etc.
我迟到了这个问题,但这确实非常简单。您只需在 css 文件中定义多个选项卡类,然后在创建 LI 标记时将所需的选项卡作为您的类加载到 php 文件中即可。
这是完全在服务器上执行此操作的示例:
CSS
PHP
I'm late to this question, but it's really super easy. You just define multiple tab classes in your css file, and then load the required tab as your class in the php file while creating the LI tag.
Here's an example of doing it entirely on the server:
CSS
PHP
假设您想更改当前选定的链接/选项卡的颜色...您最好的选择是将一个类(例如
active
)应用于当前选定的链接/tab 然后设置不同的样式。示例样式可以是:
Assuming you want to change the colour of the currently selected link/tab... you're best bet is to apply a class (say
active
) to the currently selected link/tab and then style this differently.Example style could be:
我使用 PHP 来查找 URL 并匹配页面名称(没有 .php 扩展名,我也可以添加多个具有相同单词的页面,如 contact、contactform 等。所有页面都会添加该类)并添加一个用 PHP 来改变颜色等的类。
为此,您必须使用文件扩展名
.php
保存页面。这是一个演示。根据需要更改您的链接和页面。所有链接的 CSS 类是
.tab
,对于活动链接,还有另一个.currentpage
类(与 PHP 函数一样),因此您将在其中覆盖你的 CSS 规则。您可以随意命名它们。
I use PHP to find the URL and match the page name (without the extension of .php, also I can add multiple pages that all have the same word in common like contact, contactform, etc. All will have that class added) and add a class with PHP to change the color, etc.
For that you would have to save your pages with file extension
.php
.Here is a demo. Change your links and pages as required. The CSS class for all the links is
.tab
and for the active link there is also another class of.currentpage
(as is the PHP function) so that is where you will overwrite your CSS rules.You could name them whatever you like.
试试这个。它会保留颜色,直到单击另一个项目。
Try this. It holds the color until another item is clicked.
我目前正在使用一个纯 CSS 解决方案。
添加一个正文 ID(或类)来标识您的页面和菜单项,然后使用以下内容:
HTML:
CSS:
There is a pure CSS solution I'm currently using.
Add a body ID (or class) identifying your pages and your menu items, then use something like:
HTML:
CSS:
最后,在您的帮助和帖子更改链接样式的帮助下,我终于实现了我的预期onclick。这是代码。我使用 JavaScript 来完成此操作。
At last I managed to achieve what I intended with all your help and the post Change a link style onclick. Here is the code for that. I used JavaScript for doing this.