Ver Mensaje Individual
  #6 (permalink)  
Antiguo 25/02/2013, 07:16
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 16 años, 10 meses
Puntos: 1567
Respuesta: AddEventListener me va a volver loca!

Te pongo dos ejemplos del uso de addEventListener

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <title>titulo</title>
  5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6. <script type="text/javascript">
  7. //<![CDATA[
  8. function mostrar_titulo(t){
  9. alert('title="'+ t.title + '"  href=' + t.href);
  10. }
  11.  
  12. window.onload = function(){
  13. var linksmenu = [];
  14. var contiene_links = document.getElementById('menu');
  15. linksmenu = contiene_links.getElementsByTagName('a');  
  16.     for (i=0; i<linksmenu.length; i++) {
  17.         if (linksmenu[i].addEventListener){
  18.         linksmenu[i].addEventListener("mouseover", function(){mostrar_titulo(this)}, false);
  19.         }else{ // <IE9
  20.             if (linksmenu[i].attachEvent){
  21.             linksmenu[i].attachEvent ("onmouseover", function () {mostrar_titulo(this)});
  22.         }  
  23.         }
  24.     }
  25. }
  26. //]]>
  27. </head>
  28. <div id="menu">
  29. <a href="a.html" title="titulo a">a</a><br />
  30. <a href="b.html" title="titulo b">b</a><br />
  31. <a href="c.html" title="titulo c">c</a><br />
  32. </div>
  33. </body>
  34. </html>

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <title>Submit disabled</title>
  5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6.  <script type="text/javascript">
  7. //<![CDATA[
  8. // Definimos los campos que se han de verificar, contruimos un array con los id de los mismos
  9. var campos = ["campo1", "campo2", "campo3", "campo4"];
  10. function activar() {
  11. var c = 0;
  12. for(var i in campos){
  13. i = parseInt(i);
  14. var cadenaL = document.getElementById(campos[i]).value;
  15. // hacemos un trim previo a la verificación
  16. cadenaL = cadenaL.replace(/^\s+/g,'').replace(/\s+$/g,'')
  17. if(cadenaL != ""){
  18. c++; // incrementamos c por cada campo que no está vacío
  19. }
  20. if(c == (i+1)){ // si c es = al total de los campos habilitamos el submit
  21. document.getElementById('envia').disabled = false;
  22. }else{
  23. document.getElementById('envia').disabled = true;
  24. }
  25. }
  26. }
  27.  
  28. // agregamos el evento onkeyup dinamicamente a los campos requeridos
  29. window.onload = function(){
  30.     for(var e in campos){
  31.     var elem = document.getElementById(campos[e])
  32.     if (elem.addEventListener){
  33.     elem.addEventListener("keyup", function(){activar()}, false);
  34.     }else{ // <IE9
  35.         if (elem.attachEvent){
  36.         elem.attachEvent ("onkeyup", function () {activar(elem)});
  37.         }
  38.     }
  39.     }
  40. }
  41.  
  42. //]]>
  43. </head>
  44. <form action="#" method="post">
  45. <input type="text" id="campo1" value="" /><br />
  46. <input type="text" id="campo2" value="" /><br />
  47. <input type="text" id="campo3" value="" /><br />
  48. <input type="text" id="campo4" value="" /><br />
  49. <input type="submit" id="envia" name="envia" value="procesar" disabled="disabled"/><br />
  50. </form>
  51. </body>
  52. </html>

Como ves, efectivamente, como los eventos se àgregan en la carga de la página, uso window.onload para identificar los elemento y agregar el evento.

Mi sugerencia primera es que reemplace tus funciones
Código:
function manejaGU(){
        initReq("POST","profesorGestionaUsuarios.php",true);
    }
    function manejaGE(){
        initReq("POST","profesorGestionaExamenes.php",true);
    }
    function manejaVE(){
        initReq("POST","profesorVisualizaEstadisticas.php",true);
    }
por

Código:
function manejaGU(){
        alert('usuarios');
    }
    function manejaGE(){
       alert('examenes');
    }
    function manejaVE(){
      alert('estadisticas');
    }
Una vez que te asegures que los eventos se agregan en cada boton para la función que le corresponde, analices como está trabajando initReq, que aparentemente está utilizando Ajax, pero a decir verdad no entiendo como es que la usas, en un principio si lo único que cambia es la url por que pasar tres variables.
Además te aclaro que si podés pasar variables con addEventListener por lo que directamente podrias invocar a initReq(url) en forma directa
Ejemplo de addEventListener con parámetro a la función
Código HTML:
Ver original
  1. <!DOCTYPE html>
  2. <title>addEvent + variable</title>
  3.  
  4. td{
  5. cursor: pointer;
  6. }
  7.  
  8.  
  9. // cambiatexto de id=dos
  10. function cambiatexto(nuevotexto) {
  11.   var dos = document.getElementById("dos");
  12.   dos.firstChild.nodeValue = nuevotexto;  
  13. }
  14.  
  15. // agregar Evento a tabla
  16. function iniciarEvento() {
  17.   var el = document.getElementById("tabla");
  18.   el.addEventListener("click", function(){cambiatexto("hola")}, false); // pasas un parámetro
  19. }
  20.  
  21. </head>
  22. <body onload="iniciarEvento();">
  23.  
  24. <table id="tabla">
  25.   <tr><td id="uno">uno</td></tr>
  26.   <tr><td id="dos">dos</td></tr>
  27.  
  28. </body>
  29. </html>


SAludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.