Foros del Web » Programando para Internet » Javascript »

Cambiar clase "active" a elemento seleccionado

Estas en el tema de Cambiar clase "active" a elemento seleccionado en el foro de Javascript en Foros del Web. Hola, tengo el siguiente marcado HTML: Código HTML: <ul class= "nav" > <li class= "active" > <a href= "#" > Enlace 1 </a> </li> <li> ...
  #1 (permalink)  
Antiguo 12/03/2013, 21:06
 
Fecha de Ingreso: marzo-2011
Ubicación: Caracas
Mensajes: 140
Antigüedad: 13 años, 1 mes
Puntos: 1
Cambiar clase "active" a elemento seleccionado

Hola, tengo el siguiente marcado HTML:
Código HTML:
<ul class="nav">
  <li class="active"><a href="#">Enlace 1</a></li>
  <li><a href="#">Enlace 2</a></li>
  <li><a href="#">Enlace 3</a></li>
</ul> 
y necesito cambiar la clase "active" al elemento sobre el cual haya hecho clic. He probado con los siguientes codigos:
Código:
$(function() {
    $('.nav li a').click(function(e) {
        e.preventDefault();
        var $this = $(this);
        $this.closest('ul').children('li').removeClass('active');
        $this.parent().addClass('active');
    });
});
Código:
    $('.nav li a').click(function(e) {
        e.preventDefault();
        var $this = $(this);
        $this.closest('ul').children('li').removeClass('active');
        $this.parent().addClass('active');
    });
Y no me funciona que estoy haciendo mal?
__________________
Reynier Perez Mira
Skype: reynierpm
Site: http://www.reynierpm.com
  #2 (permalink)  
Antiguo 13/03/2013, 04:35
Avatar de IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 15 años, 10 meses
Puntos: 1012
Respuesta: Cambiar clase "active" a elemento seleccionado

el primer error que cometes es usar enlaces que no redireccionan a ningún sitio.

Cita:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<style type="text/css">
.activo {color:#FF0000;}
.inactivo {color:#000000;}
</style>
<script type="text/javascript">
window.onload = function() {
divs = document.getElementsByTagName('div');
for (var i = 0; i < divs.length; i++) {
divs[i].onclick = function() {
for (var j = 0; j < divs.length; j++) {
divs[j].className = (this.id == divs[j].id) ? 'activo' : 'inactivo';
}
}
}
}
</script>
<head>
</head>
<body>
<div id="menu1" class="inactivo">menu</div>
<div id="menu2" class="inactivo">menu</div>
<div id="menu3" class="inactivo">menu</div>
<div id="menu4" class="inactivo">menu</div>
</body>
</html>
__________________
if(ViolenciaDeGénero) {alert('MUJER ASESINADA');}
  #3 (permalink)  
Antiguo 13/03/2013, 07:32
Avatar de chuidiang
Colaborador
 
Fecha de Ingreso: octubre-2004
Mensajes: 3.774
Antigüedad: 19 años, 7 meses
Puntos: 454
Respuesta: Cambiar clase "active" a elemento seleccionado

Hola:

He copiado tu código todo junto, he añadido un style para ver el cambio y parece que funciona bien, el puntito de delante de los <li> se cambia de color

Código HTML:
Ver original
  1.    <head>
  2.       <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  3.       <style>
  4.          .active {
  5.             color:red;
  6.          }
  7.       </style>
  8.    </head>
  9.    <body>
  10.       <script>
  11.       $(function() {
  12.        $('.nav li a').click(function(e) {
  13.            e.preventDefault();
  14.            var $this = $(this);
  15.            $this.closest('ul').children('li').removeClass('active');
  16.            $this.parent().addClass('active');
  17.        });
  18.       });
  19.    </script>
  20.    <ul class="nav">
  21.      <li class="active"><a href="#">Enlace 1</a></li>
  22.      <li><a href="#">Enlace 2</a></li>
  23.      <li><a href="#">Enlace 3</a></li>
  24.    </ul>
  25. </body>
  26. </html>

Se bueno.
__________________
Apuntes Java
Wiki de Programación

Etiquetas: jquery
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 22:55.