<html>
<head>
<style type="text/css">
div.showLayer {
height:60px;
width:200px;
overflow:auto;
}
</style>
<script type="text/javascript">
/**
* Hilfsfunktion für Eventhandling
* @access public
* @param Object element , welches Object den Event auslöst
* @param String type , welcher Eventhandler kommt in Frage
* @param String callBack , callBack Funktion welche den event verarbeitet
* @param Object returnParams optionale Rückgabe Parameter
* @return object event;
* @return object html object;
*/
function eventHandling(element,type,callBack,returnParams) {
/*
* eventhandler festlegen
*/
var returnParams = returnParams;
if(document.addEventListener) { //gute Browser
if(type.match(/^on/)) type = type.replace(/^on/,"");
element.addEventListener(type,handleEvent,false);
} else { // IE
if(!type.match(/^on/)) type = "on"+type;
element.attachEvent(type,handleEvent);
}
function handleEvent (evt) {
var event = (evt)?evt:(window.event)?window.event:'';
if(event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
var target = event.srcElement || event.currentTarget;
callBack.call(callBack,event,target,(returnParams)?returnParams:null);
}
}
function keynavigation (obj) {
var curElement = null;
var naviElement = null;
function clearSpaces (strOut) {
strOut = strOut.replace(/>\s+/gm,'>');
strOut = strOut.replace(/\s+</gm,'<');
strOut = strOut.replace(/>\s+</gm,'><');
return strOut;
}
function keyPressed (evt) {
var curKey = evt.keyCode;
switch (curKey) {
case 38:
if(curElement && curElement.previousSibling) {
curElement = curElement.previousSibling;
document.getElementById('check').innerHTML = curElement.firstChild.nodeValue;
}
break;
case 40:
if(curElement) {
if(curElement.nextSibling) {
curElement = curElement.nextSibling;
document.getElementById('check').innerHTML = curElement.firstChild.nodeValue;
}
} else {
curElement = naviElement.firstChild;
document.getElementById('check').innerHTML = curElement.firstChild.nodeValue;
}
break;
case 13:
if(curElement) {
alert(curElement.firstChild.nodeValue);
}
break;
default: return;
}
}
this.init = function () {
eventHandling(document,"keydown",keyPressed);
obj.innerHTML = clearSpaces (obj.innerHTML);
naviElement = obj;
}
}
window.onload = function () {
var k = new keynavigation(document.getElementById('navitree'));
k.init();
}
</script>
</head>
<body>
<div class="showLayer">
<ul id="navitree">
<li>Halle</li>
<li>Merseburg</li>
<li>Querfurt</li>
<li>Leipzig</li>
<li>Hamburg</li>
<li>Berlin</li>
</ul>
</div>
<div id="check"></div>
</body>
</html>