简体   繁体   English

如何使选定的导航按钮保持活动状态

[英]How to make selected nav button stay active

I have created some javascript trying to keep a nav button active. 我创建了一些JavaScript,试图保持导航按钮处于活动状态。 I realise this has been asked a few times before but I think my code is fairly basic and I am trying to make it work if possible. 我意识到这已经被问过几次了,但是我认为我的代码是相当基本的,如果可能的话,我正在努力使其正常工作。 My HTML looks like this: 我的HTML看起来像这样:

<nav>
<ul class ="nav"> <!-- Niall Added-->

  <li id='active1' onclick = "doClick(1)"><%= link_to "HOME",   '/' %></li>
  <li id='active2' onclick = "doClick(2)"><%= link_to "MATCHES",   matches_path %></li>
  <li id='active3' onclick = "doClick(3)"><%= link_to "CAR POOLING",   car_pooling_path %></li>
  <li id='active4' onclick = "doClick(4)"><%= link_to "STATISTICS",   statistics_path %></li>
  <li id='active5' onclick = "doClick(5)"><%= link_to "MENTORS AREA", mentors_area_path %></li>
</ul>
</nav>

And my javascript looks like this: 和我的JavaScript看起来像这样:

function doClick(x) {

if ( x == 1) {
     document.getElementById("active1").style.backgroundColor = '#99C262';

 }

 else if (x == 2) {

   document.getElementById('active2').style.backgroundColor = '#99C262';
 }

   else if (x == 3) {

   document.getElementById('active3').style.backgroundColor = '#99C262';
 } 

   else if (x == 4) {

   document.getElementById('active4').style.backgroundColor = '#99C262';
 } 

 else if (x == 5) {

   document.getElementById('active5').style.backgroundColor = '#99C262';
 } 

} }

However the button is not actually changing colour upon being clicked. 但是,单击按钮实际上并没有改变颜色。

Any advice? 有什么建议吗?

I tried your code and it works properly. 我尝试了您的代码,它可以正常工作。 But I suggest easier and more DRY solution with jQuery: 但我建议使用jQuery更简单,更干燥的解决方案:

html: HTML:

<nav>
        <ul class ="nav"> <!-- Niall Added-->
          <li class="element">Home0</li>
          <li class="element">Home1</li>
          <li class="element">Home2</li>
          <li class="element"">Home3</li>
          <li class="element">Home4</li>
        </ul>
    </nav>

css: CSS:

.element.active {
        background: #99C262;
    }

js: JS:

$(function() {
        $(".element").click(function() {
            $(this).toggleClass("active");
        });
    });

It depends on how you want to go about your solution. 这取决于您要如何解决。

I would remove the id's from the lis you don't need them you can make things a lot simplier. 我会从不需要的lis中删除id,这样可以使事情变得更加简单。

I have added an ID to the nav and removed the id's from the lis. 我已将一个ID添加到导航中,并从lis中删除了该ID。

<nav>
<ul id="nav"> <!-- Niall Added-->
  <li><%= link_to "HOME",   '/' %></li>
  <li><%= link_to "MATCHES",   matches_path %></li>
  <li><%= link_to "CAR POOLING",   car_pooling_path %></li>
  <li><%= link_to "STATISTICS",   statistics_path %></li>
  <li><%= link_to "MENTORS AREA", mentors_area_path %></li>
</ul>
</nav>

I am using loops to look at all of the child elements of the navigation and apply the event, this keeps your HTML clean. 我使用循环查看导航的所有子元素并应用事件,这使您的HTML保持整洁。

I am using the function to clear the color of all of the children then reapply the colour to the item that I clicked. 我正在使用该功能清除所有孩子的颜色,然后将颜色重新应用于我单击的项目。

(function() {

    var nav = document.getElementById('nav');

    function handleClick(e) {

        for(var i = 0; i < nav.children.length; i++) {
            nav.children[i].style.background = 'transparent';
        }
       e.target.style.background = 'red'; 
    }

    for(var i = 0; i < nav.children.length; i++) {
        nav.children[i].addEventListener('click', handleClick);
    }

})();

Check the demo here: http://codepen.io/DanielTate/pen/KVPoYa 在此处查看演示: http : //codepen.io/DanielTate/pen/KVPoYa

Obviously if you are navigating away from the page you will need to handle this in a different way, possibly looking at the URL. 显然,如果您要离开该页面,则需要以其他方式处理此问题,可能会查看URL。

I think you have to make sure your code is run on document ready. 我认为您必须确保代码可以在文档上运行。 See my example here using jQuery 在这里使用jQuery查看我的示例

.active {
  background-color: #99C262;
}

<nav>
<ul class ="nav"> 
  <li class="active">One</li>
  <li>Two</li>
  <li>Three</li>
  <li>four</li>
  <li>five</li>
</ul>
</nav>

$(function() {
  var all = $('ul li');

  all.on('click', function() {
    //clear
    all.removeClass('active');
    //set only this
    $(this).addClass('active');
  });

});

Example using jquery 使用jQuery的例子

Managed to get it working: 设法使其工作:

CSS CSS

   nav ul li a:hover {
   background-color: #080808;
   color: #ffbd10;
   transition: 0.5s;
   }

   nav ul li a.active{
   background-color:#080808;
   }

Html: HTML:

 <nav>
 <ul class ="nav"> <!-- Niall Added-->
 <li class="tab"><%= link_to "HOME",   '/' %></li>
 <li class="tab"><%= link_to "MATCHES",   matches_path %></li>
 <li class="tab"><%= link_to "CAR POOLING",   carpools_path %></li>
 <li class="tab"><%= link_to "STATISTICS",   statistics_path %></li>
 <li class="tab"><%= link_to "MENTORS AREA", mentors_area_path %></li>
 </ul>
 <div class="clearfix"></div>
 </nav>

JS: JS:

function tab_active() {
        var tabIndex = 0;
var url = location.href.toLowerCase();

if (url.indexOf("matches") > -1) {
    tabIndex = 1;
} else if (url.indexOf("carpools") > -1) {
    tabIndex = 2;
} else if (url.indexOf("statistics") > -1) {
    tabIndex = 3;
} else if (url.indexOf("mentors_area") > -1 || url.indexOf("opponents") > -1) {
    tabIndex = 4;
}

// Code using $ as usual goes here.
var $tabLinks = $(".nav .tab a");
$tabLinks.removeClass("active").eq(tabIndex).addClass("active");

$tabLinks.on("mouseover", function() {
    $tabLinks.eq(tabIndex).removeClass("active");
});
$tabLinks.on("mouseout", function() {
    $tabLinks.eq(tabIndex).addClass("active");
});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM