Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

João Rafael Martins

[Resolvido] Resumindo código

Recommended Posts

olá, escrevi um script alternando entre 4 divs, porem o script ficou muito grande e repetitivo.De que forma posso resumir o script:

function bgcolor(id) {
  if(id == 1){
   document.getElementById('id1').style.textDecoration = "underline";
document.getElementById('id2').style.textDecoration = "none";
document.getElementById('id3').style.textDecoration = "none";
document.getElementById('id4').style.textDecoration = "none";

}else if (id ==2){
document.getElementById('id1').style.textDecoration = "none";
document.getElementById('id2').style.textDecoration = "underline";
document.getElementById('id3').style.textDecoration = "none";
document.getElementById('id4').style.textDecoration = "none";
}else if (id ==3){
document.getElementById('id1').style.textDecoration = "none";
document.getElementById('id2').style.textDecoration = "none";
document.getElementById('id3').style.textDecoration = "underline";
document.getElementById('id4').style.textDecoration = "none";
}else if (id==4){
document.getElementById('id1').style.textDecoration = "none";
document.getElementById('id2').style.textDecoration = "none";
document.getElementById('id3').style.textDecoration = "none";
document.getElementById('id4').style.textDecoration = "underline";
}
}

Compartilhar este post


Link para o post
Compartilhar em outros sites
function bgcolor(id) {
  /*
     Aqui estou aplicando o estilo a todos os elementos DIV, mas se você quiser, pode no lugar usar o atributo 'class' para marcar todos os DIVs que serão alternados, para isso use o método 'getElementsByClassName'
  */
  document.getElementByTagName('div').style.textDecoration = "none";


  document.getElementById('id' + id).style.textDecoration = "underline";
}

=

Compartilhar este post


Link para o post
Compartilhar em outros sites

Uma dica é fazer com jquery também, acho bem mais simples.

 

function bgcolor(id) {

  $("div").css("text-decoration", "none");

  $("#id" + id).css("text-decoration", "underline");

}

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.