DOM Scripting vs jQuery 2: Acceso a elementos y eventos


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /*DOM Scripting*/
  2. function addLoadEvent(func) {
  3. var oldonload = window.onload;
  4. if (typeof window.onload != 'function') {
  5. window.onload = func;
  6. } else {
  7. window.onload = function() {
  8. oldonload();
  9. func();
  10. }
  11. }
  12. }
  13.  
  14. function mostrarMensaje(){
  15. if (!document.getElementsByTagName) return false;
  16. var links = document.getElementsByTagName('a');
  17. for (var i=0;i<links.length;i++) {
  18. links[i].onclick = function() {
  19. alert('Presionastes un elemento <a>');
  20. }
  21. }
  22. }
  23.  
  24. addLoadEvent(mostrarMensaje);
  25.  
  26.  
  27. /*jQuery*/
  28. $(document).ready(function(){
  29. //Aqui asignamos el click al elemento <a>
  30. $('a').click(function(){
  31. alert('Presionastes un elemento <a>');
  32. });
  33. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.