admin管理员组文章数量:1424955
On my web site every page has the same sidebar which is just a link to every page. Instead of writing the same thing on every page I'm using a file 'sidebar.html' which is then included to every page with php:
<?php include 'sidebar.html'; ?>
Now I would like to have the name of the current page in sidebar in different colour than any other page name. I have done this with JavaScript like this: 1. get the current page name by the specific id 2. set the new id called 'active' to this name and in css #active gets a new colour
I'm wondering if there is any other solution to obtain the same result and maybe if there is a solution without using JavaScript.
Thank you!
On my web site every page has the same sidebar which is just a link to every page. Instead of writing the same thing on every page I'm using a file 'sidebar.html' which is then included to every page with php:
<?php include 'sidebar.html'; ?>
Now I would like to have the name of the current page in sidebar in different colour than any other page name. I have done this with JavaScript like this: 1. get the current page name by the specific id 2. set the new id called 'active' to this name and in css #active gets a new colour
I'm wondering if there is any other solution to obtain the same result and maybe if there is a solution without using JavaScript.
Thank you!
Share Improve this question asked Nov 18, 2013 at 15:48 user2923638user2923638 251 silver badge5 bronze badges 1-
use a .php file inside, and have PHP generate a small inline
<style>
block that contains your random color. – Marc B Commented Nov 18, 2013 at 15:50
4 Answers
Reset to default 4You can replace your sidebar.html
by a sidebar.php
file, and simply use PHP variables ; for example, you can define in your current page a variable that represent the current ID, and use it inside your sidebar.php
to determine which link should be active
The other answers suggesting a PHP variable is one way to do this. On the other hand you could do this without using PHP or JavaScript, just pure CSS.
Start by indicating the current page with a class name on your <body>
tag or other high-level wrapper.
<body class="contact-page">
Then in your sidebar, make sure each link has a unique class as well. Something like:
<a href="/" class="sidebar-home">Home</a>
<a href="/contact.php" class="sidebar-contact">Contact</a>
... and so on
Now you can just write CSS classes to do the rest! So if you want your selected links to be blue for example...
.home-page .sidebar-home { color: blue; }
.contact-page .sidebar-contact { color: blue; }
You can change the html file to a php file and pass a parameter.
Then, in the new sidebar.php
, get the parameter and define the active class accordingly.
I would do something like this:
sidebar.php
$links = array();
$links['page1.html'] = 'Page 1';
$links['page2.html'] = 'Page 2';
$links['page3.html'] = 'Page 3';
$curr_page = basename($_SERVER['PHP_SELF']);
foreach($links as $k=>$v)
{
echo '<a href="'.$k.'"';
if($curr_page === $k)
{
echo ' class="active_link"';
}
echo '>'.$v.'</a>';
}
Just wanted to point out that things will get plicated if you put files into sub-directories like this:
$links = array();
$links['subdir1/page1.html'] = 'Page 1';
$links['subdir2/page2.html'] = 'Page 2';
$links['subdir3/page3.html'] = 'Page 3';
本文标签:
版权声明:本文标题:javascript - How to include sidebar with php and use different color for current page in sidebar? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745421529a2657921.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论