Jump to content

JQuery para esconder/mostrar submenus num menu


anfil89

Recommended Posts

Boas, eu estou a desenvolver um projeto universitário e gostava de saber se alguém me pode resolver um problema...

Eu tenho uma barra lateral com várias secções, e cada uma delas tem várias opções que têm a opção de estar visíveis ou não, e acontece que quando se abre a página elas estão visíveis, mas eu queria que estivessem escondidas e só ficariam visíveis quando se clica-se na secção correspondente.

// this tells jquery to run the function below once the DOM is ready
$(document).ready(function() {

// choose text for the show/hide link - can contain HTML (e.g. an image)
var showText= '<input type="image" src="../images/sidebar/arrow_show.png"/>';
var hideText= '<input type="image" src="../images/sidebar/arrow_hide.png"/>';

// initialise the visibility check
//var is_visible = false;
var check = false;

// append show/hide links to the element directly preceding the element with a class of "toggle"
$('.toggle').prev().append(' <a href="#" class="toggleLink">'+hideText+'</a>');
// hide all of the elements with a class of 'toggle'
$('.toggle').show();

// capture clicks on the toggle links
$('a.toggleLink').click(function() {

// switch visibility
//is_visible = !is_visible;

// change the link text depending on whether the element is shown or hidden
if (check) {
check = false;
$(this).html(hideText);
$(this).parent().next('.toggle').slideDown('slow');

}
else {
check = true;
$(this).html(showText);

$(this).parent().next('.toggle').slideUp('slow');
}

// return false so any link destination is not followed
return false;

});
});
Link to comment
Share on other sites

  • 2 weeks later...

Se calhar já resolveste o problema mas parece-me que existem soluções mais elegantes e com menos código.

O que precisas do JQuery é isto: http://api.jquery.com/toggle/

Penso no geral é o seguinte:

  • definir no css ou através do jquery os submenus como não visiveis
  • definir acção click (como estavas a fazer) que chama a função toggle() do JQuery
  • é só! =) se precisares
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.