Ver Mensaje Individual
  #14 (permalink)  
Antiguo 22/11/2014, 16:50
Avatar de satanson123
satanson123
 
Fecha de Ingreso: julio-2012
Mensajes: 217
Antigüedad: 11 años, 8 meses
Puntos: 2
Respuesta: usar la misma funcion mas de 1 vez

Cita:
Iniciado por javier_u Ver Mensaje
Gracias satanson. Lo he estado intentando pero el ejemplo de Alexis no lo sé aplicar. No sé nada de html mi de javascript.

Ésta es la solución de Alexis:

Código Javascript:
Ver original
  1. <img src = "imagen1.jpg" class = "foo" />
  2. <img src = "imagen2.jpg" class = "foo" />
  3. <img src = "imagen3.jpg" class = "foo" />
  4.  
  5. <button id = "bar">Cambiar tamaño de imágenes</button>


Código Javascript:
Ver original
  1. var boton = document.querySelector("#bar"),
  2.     imagenes = document.querySelectorAll(".foo"),
  3.     total = imagenes.length;
  4.  
  5. boton.addEventListener("click", function(){
  6.     for (var i = 0; i < total; i++){
  7.         if (getComputedStyle(imagenes[i]).maxWidth == "400px"){
  8.             imagenes[i].style.maxWidth = "120px";
  9.         }
  10.         else{
  11.             imagenes[i].style.maxWidth = "400px";
  12.         }
  13.     }
  14. }, false);


Le estoy dando vueltas pero no sé cómo escribir éste código para mi ejemplo:

Código Javascript:
Ver original
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>Ejemplo- FDW</title>
  6.  
  7. <script>
  8. function big_img(){
  9.       var img=document.getElementById('img-p');
  10.       if(img.style.maxWidth=="400px"){
  11.       img.style.maxWidth="120px";
  12.       }else{
  13.       img.style.maxWidth="400px";
  14.       }
  15.       }
  16. </script>
  17.  
  18. </head>
  19.  
  20. <body>
  21.   <div>
  22.     <button id = "bar" onclick="big_img(this)">Cambiar tamaño de imágenes</button>
  23.   </div>
  24.  
  25.     <img " id="img-p" src="http://www.absolut-canada.com/wp-content/uploads/2009/09/pesca-en-quebec.jpg" title="Click para ver">
  26.     <img " id="img-p" src="http://static3.absolutcaribe.com/wp-content/uploads/2009/09/playa-placencia.jpg" title="Click para ver">    
  27.  
  28. </body>
  29. </html>

Si alguien puede decirme se lo agradezco mucho. Un saludo.
Prueba asi:

Código HTML:
Ver original
  1. <!DOCTYPE html>
  2.   <meta charset="utf-8">
  3.   <title>Ejemplo- FDW</title>
  4.  <style>
  5. #img-p{
  6. max-width: 120px;
  7. }
  8. function big_img(){
  9.       var img=document.getElementById('img-p');
  10.       if(img.style.maxWidth=="400px"){
  11.       img.style.maxWidth="120px";
  12.       }else{
  13.       img.style.maxWidth="400px";
  14.       }
  15.       }
  16.  
  17. </head>
  18.  
  19.   <div>
  20.     <button id="bar" onclick="big_img();">Cambiar tamaño de imágenes</button>
  21.   </div>
  22.  
  23.     <img id="img-p" src="http://www.absolut-canada.com/wp-content/uploads/2009/09/pesca-en-quebec.jpg" title="Click para ver">
  24.     <img id="img-p" src="http://static3.absolutcaribe.com/wp-content/uploads/2009/09/playa-placencia.jpg" title="Click para ver">    
  25.  
  26. </body>
  27. </html>