Ver Mensaje Individual
  #5 (permalink)  
Antiguo 17/02/2015, 06:36
evoarte
 
Fecha de Ingreso: julio-2002
Mensajes: 813
Antigüedad: 21 años, 9 meses
Puntos: 2
Respuesta: aplicar método ON a función jquery

hola,

como me veo incapaz de aplicar correctamente el método .on() de jquery a los enlaces <a> con class .gallery que se cargan dinámicamente con ajax, he intentado ejecutar el código jquery que forma la galería una vez cargados los datos por ajax de la siguiente forma, pero sin resultado:

Código Javascript:
Ver original
  1. Cargamos el HTML recibido                           */
  2. function leerDatosTX()
  3.     {
  4.         if (oXML.readyState==1)
  5.             {
  6.                 document.getElementById("cargando").style.display= 'block';
  7.                 document.getElementById("cargando").innerHTML = '<img src="http://www.urldelapágina.com/images/ajax/ajax-loader.gif">';
  8.                 document.getElementById("documentos").style.display = 'none';
  9.             }
  10.         else if (oXML.readyState==4)
  11.             {
  12.                 var cadena = oXML.responseText;
  13.                 var mensajes = cadena.split(",");
  14.                 var accion = mensajes[0];
  15.                 if (accion == 'mensajes')
  16.                     {
  17.                         for ( i=1;i<mensajes.length;i++ )
  18.                             {
  19.                                 alert(mensajes[i]);
  20.                             }
  21.                         document.getElementById("cargando").style.display= 'none';
  22.                         document.getElementById("cargando").innerHTML= '';
  23.                         document.getElementById("documentos").style.display= 'block';
  24.                         ga('send', 'pageview', urlGO.replace("http://www.urldelapágina.com",""));
  25.                     }
  26.                 else if (accion == 'alertas')
  27.                     {
  28.                         var atextodi = "";
  29.                         for ( i=1;i<mensajes.length;i++ )
  30.                         {
  31.                             atextodi += mensajes[i] + "<br /> ";
  32.                         }
  33.                         document.getElementById("cargando").style.display= 'none';
  34.                         document.getElementById("cargando").innerHTML= '';
  35.                         document.getElementById("documentos").style.display= 'block';                      
  36.                         document.getElementById(capa).innerHTML = atextodi;
  37.                         ga('send', 'pageview', urlGO.replace("http://www.urldelapágina.com",""));
  38.                     }                  
  39.                 else if (accion == 'redireccionGU')
  40.                     {
  41.                         cadenaURLtx(mensajes[1],mensajes[2],mensajes[3]);
  42.                     }
  43.                 else if (accion == 'redireccionGO')
  44.                     {
  45.                         OajaxT(mensajes[1]);
  46.                     }
  47.                 else
  48.                     {
  49.                         document.getElementById("cargando").style.display= 'none';
  50.                         document.getElementById("cargando").innerHTML= '';
  51.                         document.getElementById("documentos").style.display= 'block';
  52.                         document.getElementById("documentos").innerHTML=oXML.responseText;
  53.                         ga('send', 'pageview', urlGO.replace("http://www.urldelapágina.com",""));
  54.                         if ( $('.gallery').length > 0 )
  55.                         {
  56.                             console.log('Hay ' + $('.gallery').length + ' elementos imagen en el documento');                      
  57.                             $('.gallery').each(function() { // the containers for all your galleries
  58.                                 $(this).magnificPopup({
  59.                                     delegate: 'a', // the selector for gallery item
  60.                                     type: 'image',
  61.                                     gallery: {
  62.                                     enabled:true
  63.                                     }
  64.                                 });
  65.                             });                        
  66.                         }                      
  67.                     }
  68.             }
  69.     }

el código que he añadido es este, no hay error, pero creo que no se ejecuta por que no se muestra el mensaje del número de imágenes (console.log):
Código Javascript:
Ver original
  1. if ( $('.gallery').length > 0 )
  2.                         {
  3.                             console.log('Hay ' + $('.gallery').length + ' elementos imagen en el documento');                      
  4.                             $('.gallery').each(function() { // the containers for all your galleries
  5.                                 $(this).magnificPopup({
  6.                                     delegate: 'a', // the selector for gallery item
  7.                                     type: 'image',
  8.                                     gallery: {
  9.                                     enabled:true
  10.                                     }
  11.                                 });
  12.                             });                        
  13.                         }

agradecería vuestra ayuda,
un saludo,
josé carlos.

Última edición por evoarte; 17/02/2015 a las 06:56