You can do it this way
Html
<ul>
<li>
<a class="active">Opção 1</a>
</li>
<li>
<a class="">Opção 2</a>
</li>
</ul>
<div class="content active">Mostrar conteúdo 1</div>
<div class="content">Mostrar conteúdo 2</div>
CSS
a {
cursor: pointer;
}
li a.active {
border-bottom: 2px solid #000;
}
.content {
display: none;
}
.content.active {
display: block;
}
Jquery
$(function(){
$('ul li a').click(function(i){
$('ul li a').removeClass('active');
$(this).addClass('active');
$('.content').each(function(index) {
$(this).toggleClass('active');
});
});
});
Clicking removes the 'active' from all 'a' and switches between contents.
Example: https://jsfiddle.net/e1dgsj6u/70/
I did everything not to change html and css, this working, but
it would be good to put an identifier for each content you want
shows, because this way will work only with two elements.
Using date for identification
HTML
<ul>
<li>
<a class="active" data-id='1'>Opção 1</a>
</li>
<li>
<a class="" data-id='2'>Opção 2</a>
</li>
</ul>
<div class="content active" data-id='1'>Mostrar conteúdo 1</div>
<div class="content" data-id='2'>Mostrar conteúdo 2</div>
I put a date-id to identify your relationship
CSS
a {
cursor: pointer;
}
li a.active {
border-bottom: 2px solid #000;
}
.content {
display: none;
}
.content.active {
display: block;
}
The same css
JQUERY
$(function(){
$('ul li a').click(function(){
$('ul li a').removeClass('active');
$('.content').removeClass('active');
$(this).addClass('active');
var id = $(this).data('id');
var content = $('.content').filter(function() {
return $(this).data("id") == id
});
content.addClass('active');
});
});
So the action is performed according to your identifier, so you can create new elements without worrying about changing the jquery.
Example: https://jsfiddle.net/e1dgsj6u/72/
Is that I want to show other content in each div.
– AnnBR
I’ll edit it and put it the way you want it
– Wictor Chaves
I changed it like you asked.
– Wictor Chaves
I don’t see the need to run . Hide and . show and the use of hasClass, when in the author’s need perhaps toggleClass would already solve
– Guilherme Nascimento
@Guilhermenascimento thanks for the tip, I made the change ;)
– Wictor Chaves