«Leistungen» имеет класс lmark, а «Impressum» - imark. Ваша функция Javascript для элементов, классифицированных как lmark, сбрасывает фон для всех других элементов, классифицированных как lmark, но не для элементов, классифицированных по imark. То же самое для функции, касающейся imark.
Если вам нужен только один фон не по умолчанию для всех элементов lmark и imark, правильный (вероятно, правильный, хотя и не проверенный) код:
function li_mark(cell) {
var elts=document.getElementById("up").getElementsByTagName("li"); //store the elements we need in a local variable so we don't have to rewrite all that line (and the browser won't have to do the fetching job again), we don't care if they're L-type or I-type
for(var i in elts)
if(elts[i].className && elts[i].className.length>=8) //check if there is a className and it is at least as long as "uplink-X", otherwise we don't process the element and skip to the next
elts[i].className="uplink-"+elts[i].className.charAt(7); //we retrieve the letter for the type of class (L or I), and we append it to "upload-" to redefine the class of the current element
cell.className+="-active"; //after the loop, all elements have their default class, so we just need to append "-active" to the one we want to be active
}
Затем все ваши звонки на l_mark(this)
и i_mark(this)
должны быть просто заменены звонками на li_mark(this)
.
P.S .: не связано, но мой текущий браузер (Firefox), похоже, не похож на window.onload=setActive;
, говоря, что "setActive не определено" ...
Редактировать в соответствии с комментариями , просмотрев ваш файл JS (scripttests.js
):
Вам по-прежнему нужны 2 функции, mark()
и li_mark()
, потому что для mark()
ваш активный класс имеет форму <className>active
вместо <className>-X-active
(не тот же шаблон в именовании классов, и нет необходимости чтобы восстановить характер Lor I).
Другая часть проблемы заключается в том, что вы вызываете cell.className+="-active"
дважды в одной и той же ячейке в одной имеющейся у вас функции.
Последняя часть проблемы заключается в том, что вы забыли инструкцию в if
второго цикла: поскольку нет квадратных скобок, инструкция, которая выполняется, когда это if
истинно, равна cell.className+="-active"
.. . выполняется столько раз, сколько выполняется цикл, и значение if
равно true.
(вероятно) рабочая версия будет:
function li_mark(cell) {
var elts=document.getElementById("up").getElementsByTagName("li"); //store the elements we need in a local variable so we don't have to rewrite all that line (and the browser won't have to do the fetching job again), we don't care if they're L-type or I-type
for(var i in elts)
if(elts[i].className && elts[i].className.length>=8) //check if there is a className and it is at least as long as "uplink-X", otherwise we don't process the element and skip to the next
elts[i].className="uplink-"+elts[i].className.charAt(7); //we retrieve the letter for the type of class (L or I), and we append it to "upload-" to redefine the class of the current element
if(cell && cell.className && cell.className.length>=8) { //if we passed an argument, then it must be a cell to activate, with a basic check on its className
cell.className+="-active"; //after the loop, all elements have their default class, so we just need to append "-active" to the one we want to be active
mark(); //we call mark() with no argument to reset all the items managed by mark(), we call it in this if-block to avoid infinite recursion
}
}
function mark(cell) {
var elts=document.getElementById("nav").getElementsByTagName("li"); //store the elements we need in a local variable so we don't have to rewrite all that line (and the browser won't have to do the fetching job again), we don't care if they're L-type or I-type
for(var i in elts)
if(elts[i].className) //check if there is a className, otherwise we don't process the element and skip to the next
elts[i].className="link"; //we set back to the default class
if(cell && cell.className) { //if we passed an argument, then it must be a cell to activate
cell.className+="active"; //after the loop, all elements have their default class, so we just need to append "active" to the one we want to be active
li_mark(); //we call li_mark() with no argument to reset all the items managed by li_mark(), we call it in this if-block to avoid infinite recursion
}
}