Autor Tema: Javascript, cambiar imagen y cambiar texto dinámicamente según elección usuario  (Leído 5602 veces)

bermartinv

  • Avanzado
  • ****
  • APR2.COM
  • Mensajes: 298
    • Ver Perfil
Hola chicos, necesito una ayuda.

Tengo el siguiente código que hace que cuando el usuario quiera se muestre un texto o se oculte, pero también quiere que cambie la imagen, cuando no está el texto la imagen tiene unas flechas hacia abajo y cuando está el texto tiene unas flechas hacia arriba. Si que consigo que aparezca el texto cuando yo quiero pero quiero cambiar la imagen y no consigo ver cómo hacerlo.


Código: [Seleccionar]
<div class="pulsador">
    <p>Ejemplo <img class="flecha" src="...."></p>
</div>
<div class="texto">
    <p>Aquí pongo el texto</p>
</div>
<div class="pulsador">
    <p>Ejemplo <img class="flecha" src="...."></p>
</div>
<div class="texto">
    <p>Aquí pongo el texto</p>
</div>
<div class="pulsador">
    <p>Ejemplo <img class="flecha" src="...."></p>
</div>
<div class="texto">
    <p>Aquí pongo el texto</p>
</div>



Y el código Javascript que tengo

Código: [Seleccionar]
window.onload = function(){
    mostrar();
}

function mostrar(){
    var items, show , imagenes;
    show = false;
    items = document.querySelectorAll(".pulsador");
    imagenes = document.querySelectorAll(".flecha");
    for ( var i = 0 ; i < items.length ; i++){
        items[i].addEventListener("click",aparecer);
    }

    function aparecer(){
        if (show == false){
        this.nextElementSibling.style.display = "block";
        show = true;
        }else{
        this.nextElementSibling.style.display = "none";
       
        show = false;
        }
    }
}

« Última modificación: 03 de Diciembre 2016, 14:52 por Ogramar »

pedro,,

  • Moderador Global
  • Experto
  • *******
  • APR2.COM
  • Mensajes: 1292
    • Ver Perfil
Re:Javascript, cambio de imagen
« Respuesta #1 en: 23 de Noviembre 2016, 13:30 »
Hola bermartinv, cuanto tiempo sin leerte.

Si no te he entendido mal, esto te valdrá, usé dos imagenes al azar, pruebaló:

Código: [Seleccionar]
     <!DOCTYPE html>
    <html>
    <head>
      <title>Ejemplo aprenderaprogramar.com JavaScript CU01188E</title>
      <meta charset="utf-8">
      <script>
    window.onload = function(){
        mostrar();
    }

    function mostrar(){
        var items, show , imagenes;
        show = false;
        items = document.querySelectorAll(".pulsador");
        imagenes = document.querySelectorAll(".flecha");
        for ( var i = 0 ; i < items.length ; i++){
            items[i].addEventListener("click",aparecer);
        }

        function aparecer(){
            if (show == false){
            this.nextElementSibling.style.display = "block";
            this.firstChild.nextSibling.firstElementChild.src = 'https://cdn.pixabay.com/photo/2014/01/29/05/49/right-254097_960_720.png';
            console.log(this.firstChild.nextSibling.firstElementChild);
            show = true;
            }else{
            this.nextElementSibling.style.display = "none";
            this.firstChild.nextSibling.firstElementChild.src = 'http://1.bp.blogspot.com/-FS6YXyFZVGU/T2FBP8QSLaI/AAAAAAAAAVs/ONlVGeQ7jG4/s400/flecha-izquierda.png';
            show = false;
            }
        }
    }
    </script>
    </head>
    <div class="pulsador">
        <p>Ejemplo <img class="flecha" alt="soy una flecha" src="http://1.bp.blogspot.com/-FS6YXyFZVGU/T2FBP8QSLaI/AAAAAAAAAVs/ONlVGeQ7jG4/s400/flecha-izquierda.png" width="60"></p>
    </div>
    <div class="texto">
        <p>Aquí pongo el texto</p>
    </div>
    <div class="pulsador">
        <p>Ejemplo <img class="flecha" alt="soy una flecha" src="http://1.bp.blogspot.com/-FS6YXyFZVGU/T2FBP8QSLaI/AAAAAAAAAVs/ONlVGeQ7jG4/s400/flecha-izquierda.png" width="60"></p>
    </div>
    <div class="texto">
        <p>Aquí pongo el texto</p>
    </div>
    <div class="pulsador">
        <p>Ejemplo <img class="flecha" alt="soy una flecha" src="http://1.bp.blogspot.com/-FS6YXyFZVGU/T2FBP8QSLaI/AAAAAAAAAVs/ONlVGeQ7jG4/s400/flecha-izquierda.png" width="60"></p>
    </div>
    <div class="texto">
        <p>Aquí pongo el texto</p>
    </div>
    </body>
    </html>


Saludos. ;D

bermartinv

  • Avanzado
  • ****
  • APR2.COM
  • Mensajes: 298
    • Ver Perfil
Re:Javascript, cambio de imagen
« Respuesta #2 en: 23 de Noviembre 2016, 14:49 »
Muchas gracias Pedro,,
funciona perfecto  ;)

 

Sobre la educación, sólo puedo decir que es el tema más importante en el que nosotros, como pueblo, debemos involucrarnos.

Abraham Lincoln (1808-1865) Presidente estadounidense.

aprenderaprogramar.com: Desde 2006 comprometidos con la didáctica y divulgación de la programación

Preguntas y respuestas

¿Cómo establecer o cambiar la imagen asociada (avatar) de usuario?
  1. Inicia sesión con tu nombre de usuario y contraseña.
  2. Pulsa en perfil --> perfil del foro
  3. Elige la imagen personalizada que quieras usar. Puedes escogerla de una galería de imágenes o subirla desde tu ordenador.
  4. En la parte final de la página pulsa el botón "cambiar perfil".