Jogger i Jabber, Programowanie (PHP, Java...), Technologie W3C (XHTML, CSS...)

Łatwe kopiowanie i ładny kod

18 czerwca, 2006 o 22:44:18 Dodaj komentarz Poziom: 0 Permalink

Zaczynam upiekszanie kodu. Najlepeiej, jeśli mialby numery linii (To nie mój pomysł).

Nad stylami muszę jeszcze popracować, ale tutaj jest próbka:

  1. function Code2Ol() {
  2.   var codes = document.getElementsByTagName("code");
  3.   for(var i = 0; i < codes.length; i++) {
  4.     var code = codes[i];
  5.     var lines = code.textContent.split('\n');
  6.     var ol = document.createElement("ol");
  7.     for(var x = 0; x < lines.length; x++) {
  8.       var li = document.createElement("li");
  9.       var li_code = document.createElement("pre");
  10.       li_code.appendChild(document.createTextNode(lines[x]));
  11.       li.appendChild(li_code);
  12.       ol.appendChild(li);
  13.     }
  14.     code.parentNode.insertBefore(ol, code);
  15.     ol.setAttribute("onclick", "ShowCode(this);");
  16.     code.setAttribute("onclick", "HideCode(this);");
  17.     HideCode(code);
  18.   }
  19. }
  20. function ShowCode(ol) {
  21.   ol.nextSibling.setAttribute("class", "code");
  22.   ol.setAttribute("class", "hide");
  23. }
  24. function HideCode(code) {
  25.   code.setAttribute("class", "hide");
  26.   code.previousSibling.setAttribute("class", "code");
  27. }

Po prostu kliknik w kod :)