Menu css + javascript and jquery

Asked

Viewed 737 times

2

Good guys, I need to create a way to recognize which menu I am clicking on, I have my index where I call my menus with a include, so I need to put a green bar at the bottom of the menu that is clicked.

How to do this ?

<ul class="mainnav">
  <li class="active"><a href="principal.php"><i class="icon-dashboard"></i><span>Início</span> </a> </li>
  <li class="dropdown"><a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown"> <i class="icon-sort"></i><span>Cadastro</span> <b class="caret"></b></a>
    <ul class="dropdown-menu">
      <li><a href="listaUsuario.php">Usuários</a></li>
      <li><a href="listaPublicacao.php">Publicação</a></li>
      <li><a href="#">Clientes</a></li>
      <li><a href="#">Paginas Internas</a></li>
    </ul>
  </li>
  <li><a href="cadastroMenu.php"><i class="icon-th-list"></i><span>Menu</span> </a></li>
  <li><a href="#"><i class="icon-picture"></i><span>Banner</span> </a> </li>
  <li><a href="#"><i class="icon-book"></i><span>Contatos</span> </a> </li>
</ul>

I just need to add active in the li class that is in selection. An example is

Be able to do, however it activates Active in the class, but it is a few seconds returning only to read marked main, the goal to start the index as Active is for it to appear checked as soon as the site is started, someone has some better idea that will solve this problem.

$(function () {
    $('.mainnav li').on('click', function(){
        $(this).addClass('dropdown active');
        $(this).siblings().removeClass('active');

    });
});
  • Is that a website? , are you using any CMS or framework to develop?

  • use the CSS selector li:hover a, has an answer that may help: http://answall.com/a/42669

  • @Erloncharles yes is a framework

  • @Sanction is not this way, I want dynamic.

  • Let me see if I understand, you want when you are on a page the link on the link on this page already come marked is this?

  • @Erloncharles get the Caio showed how to do the right way, thanks for the collaboration.

Show 1 more comment

1 answer

8


Maybe something like this is what you’re looking for?

$('.dropdown-menu li').on('click', function(){
  $(this).addClass('active');
  $(this).siblings().removeClass('active');
});
li.active{
  color: green;
}

li{
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="dropdown-menu">
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
</ul>

EDIT:

As we discussed in the comments, and as per the issue of your question (and, honestly, as per the mine understanding her), I made an adjustment in the code above, to get the result I believe you want. So as not to block the answer, I put the code in a thumb drive.

Considerations:

  • I used pretty much the same HTML that you posted on your question. I removed things like <a>s and <i>Only to make the code cleaner.
  • Use the hover, as quoted by @Sanction in the comments, it seems to me the most natural (and easiest to do) thing. I don’t know if you have any impediment to that.
  • The code does not consider (and this also does not enter the scope of your question), as you will do to open your dropdowns, so much that you will see that the class active remains present in the last internal element you clicked, if you click on an external element (I don’t know if you need to treat this). If yes, I believe that this treatment can be done together with the opening of the above dropdowns.

The secret to solve was to put the titles of the items in the first level within a tag <span>. Thus, the selectors became simpler.

  • However when clicked on index does not work.

  • 1

    Be able to solve, it was necessary to put the index as active, because it is the first to be selected.

  • This code only works for li inside .dropdown-menu, If you click on any other li, it won’t work anyway.

  • 1

    .removeClass('active')? If one day @Renanrodrigues puts another class on some item, it goes into space the day someone clicks on some menu item.

  • Excellent, @ctgPi. edited.

  • And if in case I have an ul inside the li, these items as it stands ?

  • @ctgPi Knows how to solve the problem when you click on a li inside a different one it marks the index ?

  • Okay Obg, God bless you!

  • @Caiofelipepereira managed to solve ? because now actually it doesn’t work, well, due to index, I need it to start with Active, I need also when it clicks on dropsdowns menus it remains selected the largest li.

  • @Caiofelipepereira managed to move something ?

  • @Renanrodrigues see my answer edited. anything, just ask

  • @Caiofelipe unfortunately your code is still not getting the desirable. When I click on a menu that has a link it disables Active and leaves it to index only. If you look at the site you sent me he did not fulfill the objectives. It would be possible to take another look ?

  • @Renanrodrigues but not exactly what you want? that class active stay only on the icon you clicked? If not, I suspect I’m not understanding your question. If you can explain it better, I can even try to help you

  • @Caiofelipepereira is happening the following, when I click on some menu that does not have the href, it remains clicked, however when I click on some menu that has a href, he adds the Active and soon after disabling, it is not permanent, the ideal would be to leave only after I change the page, then yes it would go to the Active of the page in which I am in question, the way it is, it gets only in the index, it goes to another more disable soon after.

  • Ah. You’re changing pages, right? When you go listaUsuario.php, for example, it loads the menu with the class active on the first link, that’s it?

  • Exact @Caiofelipepereira when I click on any link it goes back to first.

  • So the problem is different. You have to assign the class active according to the page you are on. If your menu, statically, has the index, it will be recreated every time you change page and thus return the class to index. You can create a different menu for each page, in your php, according to the icon that has to be active, or save the last element you clicked on a cookie or some kind of Log, and assign the class from there. But that way the problem became another....

Show 12 more comments

Browser other questions tagged

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