Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $menu = array(
- array(
- 'href' => 'index.php',
- 'text' => 'Index',
- ),
- array(
- 'href' => 'test.php',
- 'text' => 'Test',
- ),
- );
- ?>
- <nav>
- <ul class="menu">
- <?php
- foreach ($menu as $link) {
- $item='<li>';
- if (substr($_SERVER['REQUEST_URI'], 1) == $link['href']) {
- $item.='<a href="'.$link['href'].'" class="active">'.$link['text'].'</a>';
- } else {
- $item.='<a href="'.$link['href'].'">'.$link['text'].'</a>';
- }
- $item.='</li>';
- print $item;
- }
- ?>
- </ul>
- </nav>
- <nav>
- <ul class="menu">
- <li><a href="index.php">Index</a></li>
- <li><a href="test.php">Test</a></li>
- </ul>
- </nav>
- <script type="text/javascript">
- var menu = document.querySelectorAll('.menu li a');
- for (var i = menu.length - 1; i >= 0; i--) {
- if (menu[i].href==document.URL) {
- menu[i].setAttribute("class", "active");
- }
- };
- </script>
- <nav>
- <ul class="menu">
- <li><a href="index.php">Index</a></li>
- <li><a href="test.php">Test</a></li>
- </ul>
- </nav>
- <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
- <script type="text/javascript">
- $('.menu li a').each(function() {
- if ($(this).attr('href')==location.pathname.substr(1)){
- $(this).addClass('active');
- }
- });
- </script>
- #navigation li a.current{
- color: #ffffff;
- background:#000000;
- }
- <div id="navigation">
- <ul class="nav">
- <li><a class="current" href="index.php">Home</a></li>
- <li><a href="#">Blog</a></li>
- <li><a href="#">Members</a></li>
- <li><a href="#">About</a></li>
- </ul>
- </div>
- $(document).ready(function() {
- $('.nav li a').on('click', function(event) {
- $('.nav li a').removeClass('active').removeAttr('class');
- $(this).addClass('active');
- });
- });
- <div id="navigation">
- <ul>
- <li><a href="index.php">Home</a></li>
- <li><a href="#">Blog</a></li>
- <li><a href="#">Members</a></li>
- <li><a href="#">About</a></li>
- </ul>
- </div>
- $(function () {
- // Get the last item in the path (e.g. index.php)
- let url = window.location.pathname.split('/').pop();
- // Add active nav class based on url
- $("#navigation ul li a").each(function () {
- if ($(this).attr("href") == url || $(this).attr("href") == '') {
- $(this).closest('li').addClass("active");
- }
- })
- // Add the active class into Home if user omit index.php from url
- if (url == '') {
- $("#navigation ul li").eq(0).addClass("active");
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement