Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Rozwijane menu
Forum PHP.pl > Forum > Po stronie przeglądarki > JavaScript
rubesom
Cześć

Mam problem z rozwijanym menu. Po wklejeniu wszystkich kodów menu nie działa, nic się nie dzieje - menu po włączeniu strony jest rozwinięte.

  1. /**
  2. * @author Sławomir Kokłowski {@link <a href="http://www.kurshtml.edu.pl}" target="_blank">http://www.kurshtml.edu.pl}</a>
  3. * @copyright NIE usuwaj tego komentarza! (Do NOT remove this comment!)
  4. */
  5.  
  6. function Menu(id, style, otworz, wysun, czasRozwin, czasZwin, czasOtworz, czasZamknij, nieInicjalizuj)
  7. {
  8. if (typeof czasRozwin == 'undefined' || czasRozwin < 0) czasRozwin = 25;
  9. if (typeof czasZwin == 'undefined' || czasZwin < 0) czasZwin = 25;
  10. if (typeof czasOtworz == 'undefined' || czasOtworz < 0) czasOtworz = 250;
  11. if (typeof czasZamknij == 'undefined' || czasZamknij < 0) czasZamknij = 500;
  12.  
  13. var url = unescape(window.location.href.replace(/#.*/, ''));
  14. var base = window.location.protocol + '//' + window.location.host + window.location.pathname.replace(/[^\/\\]+$/, '');
  15.  
  16. if (style)
  17. {
  18. if (style.indexOf(':') < 0)
  19. {
  20. document.getElementById(id).className += ' ' + style;
  21. }
  22. else
  23. {
  24. style = style.replace(/(^\s+|(\s|;)+$)/g, '').split(/\s*;\s*/);
  25. for (var i = 0; i < style.length; i++)
  26. {
  27. style[i] = style[i].split(/\s*:\s*/);
  28. for (var j = 0, c, property = ''; j < style[i][0].length; j++)
  29. {
  30. c = style[i][0].charAt(j);
  31. property += c == '-' ? style[i][0].charAt(++j).toUpperCase() : c.toLowerCase();
  32. }
  33. eval('document.getElementById("' + id + '").style.' + property + ' = "' + style[i][1].replace(/"/g, '\\"') + '"');
  34. }
  35. }
  36. }
  37. for (var i = 0; i < document.getElementById(id).getElementsByTagName('dt').length; i++)
  38. {
  39. var dd = new Array();
  40. var el = document.getElementById(id).getElementsByTagName('dt')[i].nextSibling;
  41. var nodeName;
  42. while (el && (nodeName = el.nodeName.toLowerCase()) != 'dt')
  43. {
  44. if (nodeName == 'dd')
  45. {
  46. el._dt = document.getElementById(id).getElementsByTagName('dt')[i];
  47. if (otworz)
  48. {
  49. el.onmouseover = function()
  50. {
  51. clearTimeout(this._dt._timoutID);
  52. this._dt._displayed = false;
  53. this._dt.onclick();
  54. }
  55. el.onmouseout = function()
  56. {
  57. clearTimeout(this._dt._timoutID);
  58. var dt = this._dt;
  59. this._dt._timoutID = setTimeout(function () { dt._displayed = true; dt.onclick(); }, czasZamknij);
  60. };
  61. }
  62. dd[dd.length] = el;
  63. }
  64. el = el.nextSibling;
  65. }
  66. document.getElementById(id).getElementsByTagName('dt')[i]._dd = dd;
  67. document.getElementById(id).getElementsByTagName('dt')[i]._timoutID = null;
  68. document.getElementById(id).getElementsByTagName('dt')[i]._displayed = false;
  69. document.getElementById(id).getElementsByTagName('dt')[i].onclick = function()
  70. {
  71. clearTimeout(this._timoutID);
  72. if (!this._displayed)
  73. {
  74. var el = this.parentNode.getElementsByTagName('dt')[0];
  75. while (el)
  76. {
  77. if (el.nodeName.toLowerCase() == 'dt' && el != this)
  78. {
  79. el._displayed = false;
  80. if (czasZwin) display(el, 0);
  81. else display(el);
  82. }
  83. el = el.nextSibling;
  84. }
  85. }
  86. this._displayed = !this._displayed;
  87. if (this._displayed && czasRozwin || !this._displayed && czasZwin) display(this, 0);
  88. else display(this);
  89. };
  90. if (otworz)
  91. {
  92. document.getElementById(id).getElementsByTagName('dt')[i].onmouseover = function()
  93. {
  94. clearTimeout(this._timoutID);
  95. var dt = this;
  96. this._timoutID = setTimeout(function () { dt._displayed = false; dt.onclick(); }, czasOtworz);
  97. };
  98. document.getElementById(id).getElementsByTagName('dt')[i].onmouseout = function()
  99. {
  100. clearTimeout(this._timoutID);
  101. var dt = this;
  102. this._timoutID = setTimeout(function () { dt._displayed = true; dt.onclick(); }, czasZamknij);
  103. };
  104. }
  105. }
  106. start(document.getElementById(id).getElementsByTagName('dt')[0]);
  107. function start(dt)
  108. {
  109. var hide = true;
  110. var el = dt;
  111. while (el)
  112. {
  113. var nodeName = el.nodeName.toLowerCase();
  114. if (nodeName == 'dt')
  115. {
  116. dt = el;
  117. hide = true;
  118. }
  119. if (nodeName == 'dt' || nodeName == 'dd')
  120. {
  121. if (!nieInicjalizuj && el.getElementsByTagName('a').length)
  122. {
  123. var active = el.getElementsByTagName('a')[0].href && unescape(el.getElementsByTagName('a')[0].href.replace(/#.*/, '')) == url;
  124. if (!active)
  125. {
  126. var rel = el.getElementsByTagName('a')[0].getAttribute('rel');
  127. if (rel)
  128. {
  129. var matches = (' ' + rel + ' ').match(/\s+Collection\(([^)]+)\)\s+/i);
  130. if (matches)
  131. {
  132. matches = matches[1].split(',');
  133. for (var k = 0, pos = -1; k < matches.length; k++)
  134. {
  135. if (matches[k].charAt(0) == '[' && (pos = matches[k].lastIndexOf(']')) > 0)
  136. {
  137. if (new RegExp(unescape(matches[k].substring(1, pos)), matches[k].substring(pos +
  138.  
  139. 1)).test(url))
  140. {
  141. active = true;
  142. break;
  143. }
  144. }
  145. else
  146. {
  147. if (/^[\/\\]/.test(matches[k])) matches[k] = window.location.protocol + '//' +
  148.  
  149. window.location.host + matches[k];
  150. else if (!/^[a-z0-9]+:/i.test(matches[k])) matches[k] = base + matches[k];
  151. if (unescape(matches[k].replace(/[\/\\]\.([\/\\])/g,
  152.  
  153. '$1').replace(/[^\/\\]+[\/\\]\.\.[\/\\]/g, '').replace(/#.*/, '')) == url)
  154. {
  155. active = true;
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. if (active)
  164. {
  165. el.className = (el.className ? el.className + ' ' : '') + 'active';
  166. dt._displayed = true;
  167. display(dt);
  168. hide = false;
  169. var el_parentNode = el.parentNode;
  170. while (el_parentNode != document.getElementById(id))
  171. {
  172. if (el_parentNode.nodeName.toLowerCase() == 'dd')
  173. {
  174. var el_sibling = el_parentNode.previousSibling;
  175. while (el_sibling)
  176. {
  177. if (el_sibling.nodeName.toLowerCase() == 'dt')
  178. {
  179. el_sibling._displayed = true;
  180. display(el_sibling)
  181. break;
  182. }
  183. el_sibling = el_sibling.previousSibling;
  184. }
  185. }
  186. el_parentNode = el_parentNode.parentNode;
  187. }
  188. }
  189. }
  190. }
  191. if (nodeName == 'dd')
  192. {
  193. if (hide) el.style.display = 'none';
  194. start(el.getElementsByTagName('dt')[0]);
  195. }
  196. el = el.nextSibling;
  197. }
  198. }
  199.  
  200. function display(dt, i)
  201. {
  202. if (typeof i == 'undefined')
  203. {
  204. for (var i = 0; i < dt._dd.length; i++)
  205. {
  206. dt._dd[i].style.display = dt._displayed ? 'block' : 'none';
  207. if (!dt._displayed)
  208. {
  209. for (var j = 0; j < dt._dd[i].getElementsByTagName('dt').length; j++)
  210. {
  211. dt._dd[i].getElementsByTagName('dt')[j]._displayed = false;
  212. display(dt._dd[i].getElementsByTagName('dt')[j]);
  213. }
  214. }
  215. }
  216. }
  217. else if (i < dt._dd.length)
  218. {
  219. var dir = wysun ? !dt._displayed : dt._displayed;
  220. var n = dir ? i : dt._dd.length - 1 - i;
  221. dt._dd[n].style.display = dt._displayed ? 'block' : 'none';
  222. if (!dt._displayed)
  223. {
  224. for (var j = 0; j < dt._dd[n].getElementsByTagName('dt').length; j++)
  225. {
  226. dt._dd[n].getElementsByTagName('dt')[j]._displayed = false;
  227. display(dt._dd[n].getElementsByTagName('dt')[j]);
  228. }
  229. }
  230. dt._timoutID = setTimeout(function() { display(dt, i + 1); }, dt._displayed ? czasRozwin : czasZwin);
  231. }
  232. }
  233. }


  1. <dl id="menu0">
  2. <dt>Nagłówek 1</dt>
  3. <dd>
  4. <dl>
  5. <dt>Nagłówek 1.1</dt>
  6. <dd>
  7. <dl>
  8. <dt>Nagłówek 1.1.1</dt>
  9. <dd>Element 1.1.1.1</dd>
  10. <dd>Element 1.1.1.2</dd>
  11. <dd>Element 1.1.1.3</dd>
  12. <dt>Nagłówek 1.1.2</dt>
  13. <dd>Element 1.1.2.1</dd>
  14. <dd>Element 1.1.2.2</dd>
  15. <dd>Element 1.1.2.3</dd>
  16. </dl>
  17. </dd>
  18. <dt>Nagłówek 1.2</dt>
  19. <dd>Element 1.2.1</dd>
  20. <dd>Element 1.2.2</dd>
  21. <dd>Element 1.2.3</dd>
  22. </dl>
  23. </dd>
  24. <dt>Nagłówek 2</dt>
  25. <dd>
  26. <dl>
  27. <dt>Nagłówek 2.1</dt>
  28. <dd>Element 2.1.1</dd>
  29. <dd>Element 2.1.2</dd>
  30. <dd>Element 2.1.3</dd>
  31. <dt>Nagłówek 2.2</dt>
  32. <dd>Element 2.2.1</dd>
  33. <dd>Element 2.2.2</dd>
  34. <dd>Element 2.2.3</dd>
  35. </dl>
  36. </dd>
  37. </dl>


Proszę o pomoc
thek
Po wklejeniu kodu tutaj chyba samo kolorowanie składni Ci coś powinno powiedzieć. A jeśli nie to konsola JS powinna zamigać errorem. Czy naprawdę trudno załatwić sobie coś z kolorowaniem i sprawdzaniem składni? Większość darmowych edytorów nawet to ma, więc chyba czas zacząć na to zwracać uwagę. A nawet jeśli nie, to JS lint... Ludzie.... Choć trochę inicjatywy własnej :facepalm:
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.