Posted By

marcio on 12/05/08


Tagged

javascript DOM create


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

kcmr


create()


 / Published in: JavaScript
 

URL: http://www.anieto2k.com/2008/12/04/create-creacion-de-elementos-mas-rapido/#comments

  1. function extend(m, e){
  2. var e = e || this;
  3. for (var x in m) e[x] = m[x];
  4. return e;
  5. };
  6.  
  7. function create(type, opt){
  8. return extend(opt,document.createElement(type));
  9. }
  10.  
  11. //EJEMPLO
  12. create("a",{
  13. innerHTML: "Hola",
  14. href: "http://www.anieto2k.com",
  15. title: "Hola",
  16. onclick: function(){
  17. alert("Hola");
  18. }
  19. });
  20.  
  21. //---------------------------------
  22.  
  23. //Mejora para añadir el elemento al DOM directamente y poder añadir estilos
  24. function create(type, opt, parent){
  25. return ( ((el = document.createElement(type) || true) && opt.style)?extend(opt.style,el.style,delete opt.style):0 ) || 1 ? extend(opt,el,parent?parent.appendChild(el):0) : 0;
  26. }
  27.  
  28.  
  29. //EJEMPLO
  30. create("a",{
  31. innerHTML: "Hola",
  32. href: "http://www.anieto2k.com",
  33. title: "Hola",
  34. onclick: function(){
  35. alert("Hola");
  36. },
  37. style: {
  38. color: "#444",
  39. fontSize: "26px"
  40. }
  41. },document.body);

Report this snippet  

You need to login to post a comment.