Enable button according to page

Asked

Viewed 105 times

0

I have a set of Ivs below the header (cardiac, abdominal, facial...), each with its respective link, using the same names. I need that when I select one of the buttons, it becomes "active" even with the page change, because each button has its own page. Ex: I am on the page "Facials", she is active, below I have a list Collapse with some contents, with the first item also active, I click on "Vocals" the button next, the page would be changed, the button "Vocals" and the first item on the list below would be active, I am using two JS scripts, one to activate the buttons and Collapse, and another to hide a Collapse and show another depending on the item selected in the list, I thank you from now on the help! inserir a descrição da imagem aqui

<div id="botao-cardiacas-2">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="especialidades-padding img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\cardiacas.png"/>
	<p class="texto-especialidades-2">
		Cardíacas
	</p>
	<p class="texto-especialidades-sub-2">
		Problemas com bateria
	</p>
	</a>
</div>

<div id="botao-abdominais-2">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\abdominais.png"/>
	<p class="texto-especialidades-2">
		Abdominais
	</p>
	<p class="texto-especialidades-sub-2">
		Problemas com botão Home
	</p>
	</a>
</div>

<div id="botao-faciais-2">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\faciais.png"/>
	<p class="texto-especialidades-2">
		Faciais
	</p>
	<p class="texto-especialidades-sub-2">
		Problemas com a tela
	</p>
	</a>
</div>

<div id="botao-vocais-2">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\vocais.png"/>
	<p class="texto-especialidades-2">
		Vocais
	</p>
	<p class="texto-especialidades-sub-2">
		Falantes e microfones
	</p>
	</a>
</div>

<div id="botao-estetica-2">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\estetica.png"/>
	<p class="texto-especialidades-2">
		Estética
	</p>
	<p class="texto-especialidades-sub-2">
		Tampa traseira
	</p>
	</a>
</div>

<div id="botao-cognitiva-2">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="especialidades-padding img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\cognitiva.png"/>
	<p class="texto-especialidades-2">
		Cognitiva
	</p>
	<p class="texto-especialidades-sub-2">
		Antenas Wifi e GSM
	</p>
	</a>
</div>

<div id="botao-visuais-2">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="especialidades-padding img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\visual.png"/>
	<p class="texto-especialidades-2">
		Visuais
	</p>
	<p class="texto-especialidades-sub-2">
		Câmeras
	</p>
	</a>
</div>

div id="botao-respiratorio-2">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="especialidades-padding img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\respiratorio.png"/>
	<p class="texto-especialidades-2">
		Respiratório
	</p>
	<p class="texto-especialidades-sub-2">
		Dados e sistema operacional
	</p>
	</a>
</div>

<div id="botao-outros">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\outros.png"/>
	<p class="texto-especialidades-2">
		Outros
	</p>
	<p class="texto-especialidades-sub-2">
		Outros serviços
	</p>
	</a>
</div>

$( document ).ready(function() {
    $('.list-left').click(function() {
        $(this).closest('ul').find('li').removeClass('selected');
        $(this).addClass('selected');
    });
});

$( document ).ready(function() {
    $('#botao-cardiacas-2').click(function() {
        $(this).closest('ul').find('li').removeClass('selected');
        $(this).addClass('selected');
    });
});

$(document).ready(function() {
function id( el ){
        return document.getElementById( el );
}
function hide_all(){
        var divs = id('content1').getElementsByTagName('div');
        for( var i=0; i<divs.length; i++ )
        {
                divs[i].style.display = 'none';
        }
}
/* http://www.javascriptkit.com/jsref/event.shtml */
function disablelink( e ){
        var evt = window.event || e
        if (evt.preventDefault) //supports preventDefault?
                evt.preventDefault()
        else //IE browser
                return false
}
window.onload = function(){
        hide_all();
        var as = id('content1').getElementsByTagName('a');
        for( var i=0; i<as.length; i++ )
        {
                as[i].onclick = function( e ){
                        hide_all();
                        var id_el = this.href.split('#')
                       
                        id( id_el[1] ).style.display = 'block';                
                        return disablelink( e );
                }
        }
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content1" class="container-content container-height">
<ul id="test-content" class="no-padding">
<li class="list-left list1" ><a href="#item1">Reposição LCD</a></li>        
<div id="item1" class="item1">
	<p class="titulo">Reposição LDC</p>
	<p class="sub-titulo">Cirurgia facial</p>
	<p class="texto">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</p>
<img scr="#"/>
</div>
	
<li class="list-left list2"><a href="#item2">Reposição TOUCH</a></li>	
<div id="item2" class="item2">
	<p class="titulo">Reposição TOUCH</p>
	<p class="sub-titulo">Cirurgia facial</p>
	<p class="texto">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariaore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</p>
</div>
	
<li class="list-left list3"><a href="#item3">Reconexão Display / Frontal</a></li>
<div id="item3" class="item3">
	<p class="titulo">Reposição Display</p>
	<p class="sub-titulo">Cirurgia facial</p>
	<p class="texto">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in , sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</p>
</div>
	
<li class="list-left list4"><a href="#item4">Montagem MO</a></li>	
<div id="item4" class="item4">
	<p class="titulo">Montagem MO</p>
	<p class="sub-titulo">Cirurgia facial</p>
	<p class="texto">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</p>
</div>
	
<li class="list-left list5"><a href="#item5">Reposição Display Frontal</a></li>
<div id="item5" class="item5">
	<p class="titulo">Reposição Display</p>
	<p class="sub-titulo">Cirurgia facial</p>
	<p class="texto">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</p>
</div>
	
<li class="list-left list6"><a href="#item6">Aplicação Peliculas</a></li>
<div id="item6" class="item6">
	<p class="titulo">Aplicação de Peliculas</p>
	<p class="sub-titulo">Cirurgia facial</p>
	<p class="texto">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</p>
</div>
	
</ul>
</div>

  • When loading the page is reloaded or only changes the div with the desired information?

  • by me can be both ways, whatever you think best

  • When you click a button, the page reloads or everything is done dynamically without page re-load?

  • the top buttons can reload the page, only the bottom ones that do not, but if you did not load the page would also be good.

1 answer

0

If there is no page reload, you can accomplish it in the following way by just adapting the example to your need. Use bootstrap to achieve the desired accordion effect. Wrap your divs of content in a panel-group and realizes the accordion, on the buttons, you will put the id of divs which will be the targets for carrying out the.

To learn more, see here bootstrap documentation on accordions.

$('button').click( function(){
    var alvo = $(this); //captura o botao clicado
    $('button').removeClass('ativo') // remove a classe dos demais botoes
    alvo.addClass('ativo');// adiciona a classe ao botao alvo
    
})
.ativo {
   background-color: red
}
<button class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#alvo1" aria-expanded="false" > Exemplo botão 1 </button>

<button class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#alvo2" aria-expanded="false" > Exemplo botão 2 </button>
<button class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#alvo3" aria-expanded="false"> Exemplo botão 3 </button>
<button class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#alvo4" aria-expanded="false"> Exemplo botão 4 </button>


<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">  
    
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingTwo">
      <h4 class="panel-title">
        
          Eu sou o alvo 1
        </a>
      </h4>
    </div>
    <div id="alvo1" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
      <div class="panel-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingThree">
      <h4 class="panel-title">
        
          Eu sou o alvo 2
       
      </h4>
    </div>
    <div id="alvo2" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
      <div class="panel-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
   <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingThree">
      <h4 class="panel-title">
        
          Eu sou o alvo 3
       
      </h4>
    </div>
    <div id="alvo3" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
      <div class="panel-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
   <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingThree">
      <h4 class="panel-title">
        
          Eu sou o alvo 4
       
      </h4>
    </div>
    <div id="alvo4" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
      <div class="panel-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> </script>

I left the code commented, in case there are questions you can ask.

  • that I can already do, the problem is to make the top buttons change the list below that displays the contents, I have no idea how to proceed

  • @Gouper edited the answer, take a look...

Browser other questions tagged

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