简体   繁体   English

如何更改当前打开链接的文本颜色并保持未打开链接的默认颜色?这样用户就可以了解他当前所处的链接

[英]how to change the text color of currently open link and remain default color of unopened link?so user gets an idea on which link he is currently on

我想改变活动文本的颜色。所以用户知道他使用哪个页面....例如我们有2个HOME和ABOUT页面的hyberlink ..我想点击HOME链接它的颜色变化(即绿色)和其他菜单项如ABOUT将保持默认颜色(即红色)..当我点击ABOUT菜单时,它的文本应该改变(即绿色),ASLO HOME菜单将保持其默认颜色(即红色),我想在wordpress的侧边栏(在文本插件/小部件),我给了2个不同页面的超链接,所以我会更好,如果它得到HTML和CSS代码或任何其他建议将不胜感激希望你guyes将理解我的问题,提前感谢帮助我,等待积极的回应谢谢

Check out the CSS selectors :active , :hover , :link , and :visited . 查看CSS选择器:active:hover:link:visited

  • :active select and style the active link :active选择并设置活动链接的样式
  • :hover selects and styles a link with the user hovers over it :hover选择并设置链接,用户将鼠标悬停在链接上
  • :link selects and styles a link that hasn't been visited :link选择并设置未访问过的链接的样式
  • :visited selects and styles a link that has been visited :visited选择并设置已访问过的链接的样式

A :hover selector must cover after a :link and :visited selector. :hover选择器必须覆盖在:link:visited选择器之后。 Also, an :active selector must come after a :hover selector. 此外, :active选择器必须位于:hover选择器之后。

You would use these like this: 你会像这样使用这些:

a:link {
    color: red;
}

a:visited {
    color: green;
}

a:hover {
    color: blue;
}

a:active {
    color: yellow;
}

Check out this article: http://www.w3schools.com/css/css_link.asp 看看这篇文章: http//www.w3schools.com/css/css_link.asp

I'm not sure how it's done in Wordpress, but generally speaking this is how you could do it when you have to work with php includes: 我不确定它是如何在Wordpress中完成的,但一般来说,当你必须使用php时,你可以做到这一点,包括:

for every page you define a variable containing the page name, for example, in the about page you define something like 对于每个页面,您定义一个包含页面名称的变量,例如,在您定义类似的about页面中

<?php $page = "about"; ?>

and in the Home page you'd have 并在主页中你有

<?php $page = "home"; ?>

Then inside the navigation you'd check what the $page value is and echo a "active" class name for the link. 然后在导航内部,您将检查$page值是什么,并回显该链接的“活动”类名称。 I hope this explains it: 我希望这可以解释它:

<li><a href="home.php" class="<?php $page == "home" ? "active" : "" ?>">Home</a></li>
<li><a href="about.php" class="<?php $page == "about" ? "active" : "" ?>">About</a></li>

What this does is: it checks the value of the $page variable declared in the page and adds a "active" class to the anchor tag if the value matches, otherwise it leaves the class name blank. 它的作用是:它检查$page声明的$page变量的值,如果值匹配则将“active”类添加到anchor标记,否则它将类名留空。

And then in your CSS you'd have the styles for the class .active like for example: 然后在你的CSS中,你将拥有类的类型.active ,例如:

.active{ color: green; }

The point is that you need to make sure that the "current" page's anchor has a class (usually an "active" or "current" class, but you can name it anything you want), and then using php you check the name of the page as I mentioned above and then add that class name to the anchor (or list item, whatever works for you). 关键是你需要确保“当前”页面的锚点有一个类(通常是“活动”或“当前”类,但你可以将它命名为任何你想要的东西),然后使用php检查你的名字我上面提到的页面,然后将该类名添加到锚点(或列表项,任何适合您的)。

I'm not sure how you'd go about that in Wordpress, but maybe you could use the above technique and customize it somehow to fit your needs. 我不确定你是如何在Wordpress中实现的,但也许你可以使用上述技术并以某种方式定制它以满足你的需求。 (like if it's a custom widget you made you could use that) (如果它是你制作的自定义小部件,你可以使用它)

Hope that helped. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM