How to use taggleClass for 3 different css?

Asked

Viewed 39 times

0

When accessing pag, titles are color=blue

When you click on the title the color changes to color=red (and opens contents)

But when the title closes it goes back to color=blue

I need: When you click on the title change to the color=red and when you click again when you exit it becomes color=green

I appreciate the help.

JQ321
$(document).ready(function(){
    $('#abre').on('click' , function(){ // abre tudo
        $('.titulo .conteudo').show();
        });
    $('#fecha').on('click' , function(){ // fecha tudo
        $('.titulo .conteudo').hide();
    });
    $('.titulo').on('click' , function(){ // abre/fecha cada titulo
        var id = $(this).attr('id');
        $('#tit'+id).toggle();
        var classes = ['red','green']; // Nomes das classes CSS
        this.className = classes[($.inArray(this.className, classes)+1)%classes.length]; // Altera a class conforme o clique
    });
});

CSS
.titulo {color:blue}
.red {color:red}
.green {color:green}
.conteudo {display: none;}

@media(min-width:800px){
.lista-titulos {
    -webkit-column-count: 2;
    -moz-column-count: 2;
    column-count: 2;
    }
.titulo,.mostra{
    -webkit-column-break-inside: avoid;
    page-break-inside: avoid;
    break-inside: avoid;
    }
}
@media(min-width:1024px){
.titulo {cursor:pointer}
.titulo:hover {background:yellow}

PHP/HTML5
<div class="lista-titulos">
   BD select while...
  <div class="titulo" id="<?=$id?>"><span class="tit<?=$id?>"><?=$perg?>/span>
    <span id="tit<?=$id?>" class="conteudo">
        <div class="mostra">TEXTO</div>
    </span>
  </div>
</div>

NO RODAPÉ
<div><button type="button" id="abre">abrir tudo</button> <button type="button" id="fecha">fechar tudo</button></div>

1 answer

1


I believe that’s what you want.

$(document).ready(function(){
  $('#abre').on('click' , function(){ // abre tudo
    $('.titulo .conteudo').show();
  });
  $('#fecha').on('click' , function(){ // fecha tudo
    $('.titulo .conteudo').hide();
  });
  $('.titulo').on('click' , function(){ // abre/fecha cada titulo
    var id = $(this).attr('id');
    $('#tit'+id).toggle();
    $(this).toggleClass(function() {
      // Altera a classe CSS
      return $(this).is('.red, .green') ? 'red green' : 'red';
    });
  });
});
.titulo {color:blue}
.red {color:red}
.green {color:green}
.conteudo {display: none;}

@media(min-width:800px){
.lista-titulos {
    -webkit-column-count: 2;
    -moz-column-count: 2;
    column-count: 2;
    }
.titulo,.mostra{
    -webkit-column-break-inside: avoid;
    page-break-inside: avoid;
    break-inside: avoid;
    }
}
@media(min-width:1024px){
.titulo {cursor:pointer}
.titulo:hover {background:yellow}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="titulo" id="01">
  <span class="cor01">TITULO 01</span>
    <span id="tit01" class="conteudo">Conteudo 01</span>
</div>
<div class="titulo" id="02">
  <span class="cor02">TITULO 02</span>
    <span id="tit02" class="conteudo">Conteudo 02</span>
</div>
<div class="titulo" id="03">
  <span class="cor03">TITULO 03</span>
    <span id="tit03" class="conteudo">Conteudo 03</span>
</div>
<div class="titulo" id="04">
  <span class="cor04">TITULO 04</span>
    <span id="tit04" class="conteudo">Conteudo 04</span>
</div>
<div><button type="button" id="abre">abrir tudo</button> <button type="button" id="fecha">fechar tudo</button></div>

  • Wéllingthon, thanks for the quick response, its function is perfect, but is conflicting with the function opens/closes all titles. (Qdo opens individual and then click open all, gets confused) I did a full function update, css and html. I’m sorry I didn’t inform you correctly. I appreciate your attention and time spent Abs.

  • @Geo see now, what was happening is that clicking on the title changed the value of the attribute Class of class="titulo" for class="red" hence the jQuery couldn’t find the dial .titulo.

  • is perfect. NOTE 1000 for your attention and patience. I analyzed the code and learned a little more. A hug and many thanks.

  • I am new here and I saw that my question is -1. Was it very basic, poorly formulated or out of context? Who scores is the Administrator? Just curious rsrs. Abs and thanks again, it was great!!!!

  • Behold Help Center > Asking, you can improve it by formatting, and explaining your problem, etc. Look at this link I passed.

Browser other questions tagged

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