DOM Scripting vs jQuery 3: Añadir una clase


/ 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 addClass(element,value) {
  15. if (!element.className) {
  16. element.className = value;
  17. } else {
  18. newClassName = element.className;
  19. newClassName+= " ";
  20. newClassName+= value;
  21. element.className = newClassName;
  22. }
  23. }
  24.  
  25. function aplicarEstilo(){
  26. if (!document.getElementById) return false;
  27. var enlace = document.getElementById('enlace');
  28. enlace.onclick = function(){
  29. var parrafo = document.getElementById('parrafo');
  30. addClass(parrafo,'resaltar');
  31. }
  32. }
  33.  
  34. addLoadEvent(aplicarEstilo);
  35.  
  36.  
  37. /*jQuery*/
  38. $(document).ready(function(){
  39. $('#enlace').click(function(){
  40. $('#parrafo').addClass('resaltar');
  41. });
  42. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.