简体   繁体   English

在引导程序中更改活动列表项的背景颜色

[英]Change Background color of Active list item in bootstrap

I have item group list我有项目组列表

<div id="MainMenu">
    <div class="list-group panel">
        <a href="#why" class="list-group-item" data-toggle="collapse" data-parent="#MainMenu">Menu 1</a>
        <div class="collapse" id="why">
            <a href="" class="list-group-item" data-toggle="collapse" data-parent="#SubMenu1">Menu 1 a</a>
            <a href="" class="list-group-item">Menu 1 b</a>
            <a href="" class="list-group-item">Menu 1 c</a>
            <a href="" class="list-group-item">Menu 1 d</a>
            <a href="" class="list-group-item">Menu 1 e</a>
            <a href="" class="list-group-item">Menu 1 f</a>
        </div>
        <a href="#joinus" class="list-group-item" data-toggle="collapse" data-parent="#MainMenu">Menu 2</a>
        <div class="collapse" id="joinus">
            <a href="" class="list-group-item">Menu 2 a</a>
            <a href="" class="list-group-item">Menu 2 b</a>
            <a href="" class="list-group-item">Menu 2 c</a>
            <a href="" class="list-group-item">Menu 2 d</a>
            <a href="" class="list-group-item">Menu 2 e</a>
        </div>
    </div>
</div>

I want to change background of active list item, I Know how to change background, but I am unable to get which list is active, or inactive by JavaScript, tried lots of solution given on others but didn't woJrk.我想更改活动列表项的背景,我知道如何更改背景,但我无法通过 JavaScript 获取哪个列表处于活动状态或不活动状态,尝试了很多其他人给出的解决方案,但没有成功。

JsFiddle提琴手

UPDATE:更新:

Don't know why bootstrap isn't doing it, but here's some jQuery on a fiddle for you.不知道为什么 bootstrap 不这样做,但这里有一些 jQuery 为您提供了一个小提琴 The alert is displaying the href that is active.警报显示活动的href

Is that what you're after?这就是你追求的吗?

Add the following css in your code as :在您的代码中添加以下 css 为:

.list-group-item[aria-expanded="true"]{
     background-color: black !important;
  border-color: #aed248;

}

Demo演示

The solution is simple but maybe not obvious.解决方案很简单,但可能并不明显。 You can pass this (the clicked element) to an onclick event handler and then set the active class on the selected menu.您可以this (单击的元素)传递给onclick事件处理程序,然后在所选菜单上设置active类。

var activate = function(el) {    
    var current = document.querySelector('.active');
    if (current) {
        current.classList.remove('active');
    }
    el.classList.add('active');
}

I created this Fiddle to answer your question我创建了这个小提琴来回答你的问题

http://jsfiddle.net/Ltp9qLox/9/ http://jsfiddle.net/Ltp9qLox/9/

The script can be greatly improved, this is just an example.脚本可以大大改进,这只是一个例子。 I'm not aware of any non-JS way to achieve the same result.我不知道有任何非 JS 方法可以达到相同的结果。

You can also store the old activated element so you don't have to use query selector every time, in this way the script would be您还可以存储旧的激活元素,这样您就不必每次都使用查询选择器,这样脚本将是

var current;
var activate = function(el) {    
    if (current) {
        current.classList.remove('active');
    }
    current = el;
    el.classList.add('active');
}

Bu then you have to initialize current with the value of the starting element.然后你必须用起始元素的值初始化current

Adding Persistency添加持久性

Of course any change to the style of an element can't survive after a refresh without implementing some kind of persistency that is something completely different than the simple implementation.当然,如果不实现某种与简单实现完全不同的持久性,则元素样式的任何更改都无法在刷新后继续存在。 Keep in mind that there are hundreds of different ways to achieve this, one of which is NOT refreshing at all the page .请记住,有数百种不同的方法可以实现这一点,其中之一是根本不刷新页面

Anyway if you prefer the quick and dirt way then using localStorage is probably the best solution.无论如何,如果您更喜欢快速而肮脏的方式,那么使用 localStorage可能是最好的解决方案。 This is a simple implementation这是一个简单的实现

var currentHref = localStorage.getItem("currentSelected");
var current = currentHref ? document.querySelector('[href="'+currentHref+'"]') : null;
function activate(el) {    
    if (current && current !== el) {
        current.classList.remove('active');
    }
    current = el;
    current.classList.add('active');
    localStorage.setItem("currentSelected", current.getAttribute('href'));
}

Basically you save something that you can use to recognize the element that was selected, in this case i used the href attribute value because in our case that is unique, but you could also assign id or other attributes to the elements and use that.基本上,您保存了一些可以用来识别所选元素的东西,在这种情况下,我使用了href属性值,因为在我们的情况下它是唯一的,但您也可以为元素分配 id 或其他属性并使用它。 Then on load i read the localStorage to retrieve the saved href and if found i get the element inside the page using a simple querySelector .然后在加载时我读取 localStorage 以检索保存的href ,如果找到我使用简单的querySelector获取页面内的元素。

Just remember that copy-pasting this kind of solution doesnt help you building better websites, you should read articles on the internet and implement what solution is best for your own use case.请记住,复制粘贴这种解决方案并不能帮助您构建更好的网站,您应该阅读互联网上的文章并实施最适合您自己用例的解决方案。

What i does it assign and id to every link in list that is also the page name, and made a js function that is called on body load of the page.我所做的是为列表中的每个链接分配和 id,也是页面名称,并创建了一个在页面主体加载时调用的 js 函数。 the function get the current file name from url and determines which page is this, then by js i made that link class active.该函数从 url 获取当前文件名并确定这是哪个页面,然后通过 js 我使该链接类处于活动状态。 this solve my problem.这解决了我的问题。 however i share this solution for others to improvement.但是我分享了这个解决方案供其他人改进。

 function get_current_page() { var pathArray = window.location.pathname.split("/"); var current_page = pathArray[pathArray.length - 1]; current_page_array = current_page.split("."); current_page = current_page_array[0]; if ( current_page == "students" || current_page == "my-profile" || current_page == "faqs" || current_page == "forecast-career" ) { document.getElementById("joinuslist").className += " active"; document.getElementById("joinus").className += " in"; if (current_page == "students") { document.getElementById("students").className += " active"; } else if (current_page == "faqs") { document.getElementById("faqs").className += " active"; } else if (current_page == "forecast-career") { document.getElementById("forecast-career").className += " active"; } else if (current_page == "my-profile") { document.getElementById("my-profile").className += " active"; } else { } } else if ( current_page == "values" || current_page == "ambassadors" || current_page == "documentary" ) { if (current_page == "values") { document.getElementById("values").className += " active"; } else if (current_page == "ambassadors") { document.getElementById("ambassadors").className += " active"; } else if (current_page == "documentary") { document.getElementById("documentary").className += " active"; } else { } } }
 .list-group-item.active:hover { background-color: #aed248 !important; border-color: #aed248 !important; } .list-group-item.active, .list-group-item.active:hover { background-color: #007715 !important; border-color: #aed248 !important; } #joinus .list-group-item.active, .list-group-item.active:hover { background-color: #adce1b !important; border-color: #adce1b !important; } #whyptcl .list-group-item.active, .list-group-item.active:hover { background-color: #adce1b !important; border-color: #adce1b !important; } .panel { margin-bottom: 20px; background-color: transparent !important; border: 0px solid transparent; border-radius: 5px; -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05); box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05); }
 <body onload="get_current_page()"> <div id="MainMenu"> <div class="list-group panel"> <a id="whylist" href="#why" class="list-group-item" data-toggle="collapse" data-parent="#MainMenu" >Menu 1</a > <div class="collapse" id="why"> <a id="values" href="values.html" onclick="activate(this)" class="list-group-item" data-toggle="collapse" data-parent="#SubMenu1" >Menu 1 a</a > <a id="ambassadors" href="ambassadors.html" onclick="activate(this)" class="list-group-item" >Menu 1 b</a > <a id="documentary" href="documentary.html" onclick="activate(this)" class="list-group-item" >Menu 1 c</a > </div> <a id="joinuslist" href="#joinus" class="list-group-item" data-toggle="collapse" data-parent="#MainMenu" >Menu 2</a > <div class="collapse" id="joinus"> <a id="my-profile" href="my-profile.html" onclick="activate(this)" class="list-group-item" >Menu 2 a</a > <a id="students" href="students.html" onclick="activate(this)" class="list-group-item" >Menu 2 b</a > <a id="forecast-career" href="forecast-career.html" onclick="activate(this)" class="list-group-item" >Menu 2 c</a > <a id="faqs" href="faqs.html" onclick="activate(this)" class="list-group-item" >Menu 2 e</a > </div> </div> </div> </body>

Just to change the active item background color, (I've changed to grey from default - blue) add this to your css:只是为了更改活动项目的背景颜色,(我已从默认更改为灰色 - 蓝色)将其添加到您的 css 中:

.list-group-item.active {
background-color: grey;
border-color: grey; }

You can add these Bootstrap classes;您可以添加这些 Bootstrap 类;

.list-group-item-dark
.list-group-item-success
.list-group-item-warning
.list-group-item-primary
.list-group-item-danger
.list-group-item-secondary
.list-group-item-info

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

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