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?
worked but the active class back to start
– user76271
Could you help me? I made some modifications but it didn’t help.
– user76271
put a
return false
, now hangs on . Nav-link clicked, but the URL does not work.– user76271
You are browsing with ajax or refresh ?
– AnthraxisBR
by get, actually using MVC that I rode for study.
– user76271
But the firing of these links, does what ? it updates the page, or loads the content into a div without updating?
– AnthraxisBR
It updates the page, I do not use ajax
– user76271
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.
– AnthraxisBR
she gets like this:
localhost/mvc/about
for example, there’s this one toolocalhost/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.– user76271
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.
– AnthraxisBR
Have an example with PHP ?
– user76271
See the response update
– AnthraxisBR
worked haha. vlw
– user76271