Ver Mensaje Individual
  #2 (permalink)  
Antiguo 01/02/2010, 05:58
Avatar de Adler
Adler
Colaborador
 
Fecha de Ingreso: diciembre-2006
Mensajes: 4.671
Antigüedad: 17 años, 3 meses
Puntos: 126
Respuesta: Detectar el elemento que lanza el evento

Hola

Sí, mírate este ejemplo

Código Javascript:
Ver original
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var ns4 = (document.layers)? true:false
  5. var ie4 = (document.all)? true:false
  6. var ns6 = (document.getElementById)? true:false
  7.  
  8. var info = {
  9.  
  10. captura_objeto : function(idnombre) {
  11.     if (ns6)
  12.     {
  13.     return document.getElementById(idnombre);
  14.     }
  15.     else if (ie4)
  16.     {
  17.     return document.all[idnombre];
  18.     }
  19.     else if (ns4)
  20.     {
  21.     return document.layers[idnombre];
  22.     }
  23.     else
  24.     {
  25.     return null;
  26.     }
  27. },
  28.  
  29. Evento : function(elemento,nomevento,funcion) {
  30.     if (elemento.attachEvent)
  31.     {
  32.         var fct=function(){
  33.             funcion.call(elemento,window.event);
  34.         }
  35.         elemento.attachEvent('on'+nomevento,fct);
  36.         return true;
  37.     }
  38.     else  
  39.         if (elemento.addEventListener)
  40.         {
  41.         elemento.addEventListener(nomevento,funcion,false);
  42.         return true;
  43.         }
  44.         else
  45.         return false;
  46. },
  47.  
  48.  
  49. PosicionAbsolutaElemento: function(element)  {
  50.     if (typeof element == "string")
  51.         element = info.captura_objeto(element)
  52.             if (!element) return { top:0,left:0 };
  53.         var y = 0;
  54.         var x = 0;
  55.             while (element.offsetParent) {
  56.                     x += element.offsetLeft;
  57.                     y += element.offsetTop;
  58.                 element = element.offsetParent;
  59.      }
  60.     return {top:y,left:x};
  61.  
  62. },
  63.    
  64.  
  65. init: function(){
  66. var ref = info.captura_objeto('lasLigas').getElementsByTagName('A');
  67. for (var i = 0; i < ref.length; i++) {
  68. //alert(ref[i].id)
  69. var infodiv = info.captura_objeto('info_menu_'+ref[i].id);
  70.     //infodiv.style.width = '42%';
  71.     info.altura = infodiv.clientHeight;
  72.     infodiv.style.display = 'none';
  73.     //infodiv.style.overflow = 'hidden';
  74.     //infodiv.style.zIndex = 1000;
  75.             info.Evento(ref[i], 'click', function() {info.efecto(this.id)});
  76. }
  77. },
  78.  
  79.  
  80.    
  81. efecto: function(id){
  82. info.captura_objeto('info_menu_'+id).style.display == 'none'
  83. //alert("Estado - " + info.abierto);
  84.             if(info.captura_objeto('info_menu_'+id).style.display == 'none'){
  85. //alert("Estado - true");
  86.                     clearInterval(info.intervalo);
  87.                 info.muestra(id);
  88.         info.abierto = false;
  89.             }else{
  90. //alert("Estado - false");
  91.                     clearInterval(info.intervalo);
  92.                     info.oculta(id);
  93.         info.abierto = true;
  94.             }
  95. },
  96.    
  97.  
  98.  
  99. muestra: function(id){
  100. //alert(id)
  101. var infodiv = info.captura_objeto('info_menu_'+id);
  102. var PosElemento = info.PosicionAbsolutaElemento(id);
  103. //alert("Left - " +PosElemento.left+ " Top - " + PosElemento.top);
  104.     infodiv.style.height = '1px';
  105.     infodiv.style.left = PosElemento.left +'px';
  106.     infodiv.style.top = parseInt(PosElemento.top+21) +'px';
  107.             infodiv.style.display = 'block';
  108.             info.intervalo = setInterval(function(){ info.redimenciona(info.altura,id); }, 1);
  109. },
  110.    
  111.  
  112.  
  113. oculta: function(id){
  114. var infodiv = info.captura_objeto('info_menu_'+id);
  115.             infodiv.style.height = info.altura + 'px';
  116.             info.intervalo = setInterval(function(){ info.redimenciona(0,id); }, 1);
  117. },
  118.    
  119.  
  120.  
  121. redimenciona: function(limite,id){
  122. var infodiv = info.captura_objeto('info_menu_'+id);
  123.     inicial = parseInt(infodiv.style.height);
  124.             actual = Math.ceil((limite - inicial)/4);
  125.             inicial += actual;
  126.             infodiv.style.height = inicial + 'px';
  127.         if (parseInt(inicial) >= (limite - 5) && parseInt(inicial) <= (limite + 5)) {
  128.             if (info.abierto == false) {
  129.                 infodiv.style.height = limite + 'px';
  130.                 clearInterval(info.intervalo);
  131.             } else {
  132.                 infodiv.style.height = '0px';
  133.                 infodiv.style.display = 'none';
  134.                 clearInterval(info.intervalo);
  135.             }
  136.         }
  137. },
  138.    
  139. abierto: true
  140. }
  141.  
  142. info.Evento(window, 'load', info.init);
  143. </script>
  144. </head>
  145. <body>
  146. <div id="lasLigas">
  147. <a href="javascript:void(0);" id="1">Mostrar Menú 1</a>
  148. <a href="javascript:void(0);" id="2">Mostrar Menú 2</a>
  149. <a href="javascript:void(0);" id="3">Mostrar Menú 3</a>
  150. <a href="javascript:void(0);" id="4">Mostrar Menú 4</a>
  151. </div>
  152.  
  153. <div id="info_menu_1" style="position:absolute; left:0px; top:0px; width:42%; overflow:hidden; z-index:1000; font:bold 11px Arial, Helvetica, sans-serif; background-color:#EEEEEE; border: 1px solid #C9C9C9; border-width: 1px 1px 1px 1px;">
  154. Un texto que quiero ocultar y mostrar con el botón<br />
  155. Un texto que quiero ocultar y mostrar con el botón<br />
  156. Un texto que quiero ocultar y mostrar con el botón<br />
  157. Un texto que quiero ocultar y mostrar con el botón<br />
  158. Un texto que quiero ocultar y mostrar con el botón<br />
  159. Un texto que quiero ocultar y mostrar con el botón
  160. </div>
  161.  
  162.  
  163. <div id="info_menu_2" style="position:absolute; left:0px; top:0px; width:42%; overflow:hidden; z-index:1000; font:bold 11px Arial, Helvetica, sans-serif; background-color:#EEEEEE; border: 1px solid #C9C9C9; border-width: 1px 1px 1px 1px;">
  164. Un texto que quiero ocultar y mostrar con el botón<br />
  165. Un texto que quiero ocultar y mostrar con el botón<br />
  166. Un texto que quiero ocultar y mostrar con el botón<br />
  167. Un texto que quiero ocultar y mostrar con el botón<br />
  168. Un texto que quiero ocultar y mostrar con el botón<br />
  169. Un texto que quiero ocultar y mostrar con el botón
  170. </div>
  171.  
  172.  
  173. <div id="info_menu_3" style="position:absolute; left:0px; top:0px; width:42%; overflow:hidden; z-index:1000; font:bold 11px Arial, Helvetica, sans-serif; background-color:#EEEEEE; border: 1px solid #C9C9C9; border-width: 1px 1px 1px 1px;">
  174. Un texto que quiero ocultar y mostrar con el botón<br />
  175. Un texto que quiero ocultar y mostrar con el botón<br />
  176. Un texto que quiero ocultar y mostrar con el botón<br />
  177. Un texto que quiero ocultar y mostrar con el botón<br />
  178. Un texto que quiero ocultar y mostrar con el botón<br />
  179. Un texto que quiero ocultar y mostrar con el botón
  180. </div>
  181.  
  182.  
  183. <div id="info_menu_4" style="position:absolute; left:0px; top:0px; width:42%; overflow:hidden; z-index:1000; font:bold 11px Arial, Helvetica, sans-serif; background-color:#EEEEEE; border: 1px solid #C9C9C9; border-width: 1px 1px 1px 1px;">
  184. Un texto que quiero ocultar y mostrar con el botón<br />
  185. Un texto que quiero ocultar y mostrar con el botón<br />
  186. Un texto que quiero ocultar y mostrar con el botón<br />
  187. Un texto que quiero ocultar y mostrar con el botón<br />
  188. Un texto que quiero ocultar y mostrar con el botón<br />
  189. Un texto que quiero ocultar y mostrar con el botón
  190. </div>
  191. </body>
  192. </html>

Suerte
__________________
Los formularios se envían/validan con un botón Submit
<input type="submit" value="Enviar" style="background-color:#0B5795; font:bold 10px verdana; color:#FFF;" />