Hi I gave myself a crash course in bootstrap last night and I'm having a lot of fun with it. I found online a collapsible panel that works really well for what I am hoping to achieve in my navbar. However I want the background color of the panel heading to change when expanded. I am not familiar enough with the inner workings of bootstrap yet to noodle it out on my own (soon! I hope). Code in question is below:
<div class="panel-group minmarg" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
OPI<b class="caret"></b>
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body minmarg">
<ul class="nav nav-pills nav-stacked pill-trngtheme minmarg">
<li role="presentation"><a href="#">OPI Main Site</a>
</li>
<li role="presentation" class="active"><a href="#">Training</a>
</li>
<li role="presentation"><a href="#">Procedures</a>
</li>
<li role="presentation"><a href="#">Corrective Action</a>
</li>
<li role="presentation"><a href="#">Human Performance Improvement</a>
</li>
<li role="presentation"><a href="#">LEAN Management</a>
</li>
<li role="presentation"><a href="#">Coduct of Operations</a>
</li>
</ul>
</div>
</div>
</div>
</div>
AngularJR answer is correct but I found it a little hard to understand with my limited knowledge of css and non-existent knowledge of jQuery. Plus I am using react and so tweaking DOM elements after they have been rendered wasn't a good approach for me.
Core Principle
The basic principle of what is going on is that an extra class tag is being added to the html element:
So
becomes
It doesn't really matter how you add in these extra classes, jQuery (
$
) is just one option.Different css rules can now be applied to these two divs using the css selectors:
and
respectively.
Notice that there is no space between
.panel-heading
and.on
. See the following stack overflow question for more information: What's the difference between CSS classes .foo.bar (without space) and .foo .bar (with space).My aside about solving the problem when using react
However in the case of using react-bootstrap the
"panel-heading"
div is created automatically by the tag<Panel>
and the<div class="panel-heading">
element can't be searched for and accessed after it has been rendered (the react life cycle is weird). So no extra classes can be added to it.So the extra class,
"on"
, needs to be added to a parent element of thepanel-heading
div:html (or .jsx):
results in a DOM that looks like this (slightly simplified):
There is now an outer div with the
"on"
class and the"panel-heading"
div is contained within it.The panel-heading div can now be styled in css using the following selectors:
and
Notice how the
.on
selector is now before the.panel-heading
selector and there is also a space between the two selectors; this is because the element with the.on
class is now a ancestor of the element with the.panel-heading
class.Note: Adding the extra
"on"
class directly to<Panel>
and not enclosing the panel in an extra div would work similarly.Now the "onClick" handler can change the class of the parent element to affect the styling of the panel-heading, which is much easier than searching the DOM for the correct
"panel-heading div"
(IMHO). And crucially, in my case of using react, this"on"
class can be specified directly for an element when calling the render routine.Webforwork, Hi there.
How about something like this and change the class, using
toggleclass
.CSS
JS
Hope this helps you.
You can reference to document
Here code for you: