Bootstrap . Nav-pills active

Asked

Viewed 871 times

1

I have the code, which by clicking another link opens the page and adds the class .active I have tried several scripts and nothing works.

<nav>
      <ul class="nav nav-pills float-right">
        <li class="nav-item">
          <a class="nav-link active" href="<?php echo Url::url_base(); ?>">Início</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="<?php echo Url::url_base(); ?>/about">Sobre</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="<?php echo Url::url_base(); ?>/error">Erro</a>
        </li>
      </ul>
    </nav>

When clicking on or in error, wanted to remove the class active of beginning and add to the clicked link. How to do?

$(document).on('click','.nav-link', function() {
			    $(this).closest('.nav-pills').find('.nav-link').removeClass('active');
			    $(this).addClass('active');
			    return true;
			});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>

<header class="header clearfix">
  <div class="container">
    <nav>
      <ul class="nav nav-pills float-right">
        <li class="nav-item">
          <a class="nav-link active" href="home">Início</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="about">Sobre</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="error">Erro</a>
        </li>
      </ul>
    </nav>
    <h3 class="text-muted">Title Brand</h3>
  </div>
</header>

He even switches but returns to class active pro Início. Someone give me a light?

1 answer

0


Go through the elements 'li', remove classname 'active' from the previous, and set to the current.

$(document).on('click','.nav-link',function() {
    $(this).closest('.nav-pills').find('.nav-link').removeClass('active');
    $(this).addClass('active');
})

But if your browsing is refreshed, you will need to pass some parameter in the url to be identified on the other side and mark the related element as 'active'.

Making with php:

Suppose your url looks like this after clicking:

http://localhost/mvc/index.php? page=about

Would look like this:

<?php
$pg = $_GET['pagina'];
?>
<nav>
      <ul class="nav nav-pills float-right">
        <li class="nav-item">
          <a class="nav-link active" href="<?php echo Url::url_base(); ?>">Início</a>
        </li>
        <li class="nav-item">

          <a class="nav-link <?php if($pg == 'sobre') echo 'active' ?>" href="<?php echo Url::url_base(); ?>/about">Sobre</a>
        </li>
        <li class="nav-item">
          <a class="nav-link <?php if($pg == 'erro') echo 'active' ?>" href="<?php echo Url::url_base(); ?>/error">Erro</a>
        </li>
      </ul>
    </nav>
  • worked but the active class back to start

  • Could you help me? I made some modifications but it didn’t help.

  • put a return false, now hangs on . Nav-link clicked, but the URL does not work.

  • You are browsing with ajax or refresh ?

  • by get, actually using MVC that I rode for study.

  • But the firing of these links, does what ? it updates the page, or loads the content into a div without updating?

  • It updates the page, I do not use ajax

  • So in this case, it’s better to do it with php, what does your url look like after clicking on the link ? So I know how to update the answer.

  • she gets like this: localhost/mvc/about for example, there’s this one too localhost/mvc/erro, I tried to make it with $_GET but since I’m using MVC it gets really ugly, I think javascript is the best solution but I can’t.

  • Without browsing with ajax you will not be able to do this with javascript/jQuery, you will need the parameters in the URL to identify what are the changes and what is the new page, and to identify the 'active' element by the parameter with php.

  • Have an example with PHP ?

  • See the response update

  • worked haha. vlw

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.