mumebuhi a écrit :
Hi,
How do you make an element (e.g. <div>) to receive focus when pressing
TAB key? I understand that some elements such as <select> and <input>
by default will receive focus when the TAB key is pressed.
and more useful with the attribute 'tabindex'
Is there a way to tweak a <div> to get the same treatment?
Not in html.
perhaps could you try with an anchor in this div ?
(that doesn't work on my Fx)
What do you expect to do ?
Tab to only one specific div ?
or to jump by tabbing from div to div ?
In all cases you'll have to catch the keycode of tab,
something like:
<body onkeydown="KeyCheck(event);" onkeyup="if(cod==9)jump();">
Your div(s) must all of then have an id.
<script type="text/javascript">
var cod=0, goal, D=[];
function KeyCheck(evt) {
evt = (evt) ? evt : ((event) ? event : null);
var evver = (evt.target) ? evt.target : ((evt.srcElement)
?evt.srcElement : null );
cod = evt.keyCode;
goal = evver
}
function init() {
var d = document.getElementsByTagName('DIV');
for(var i=0, n<d.length; i<n; i++) {
d
.idx = i;
D = d;
}
}
window.onload = init;
function jump() {
if(goal && (!goal.tagName || goal.tagName != 'DIV')
goal = goal.parentNode;
while(goal.tagName!='DIV') goal = goal.parentNode;
var i = (goal.idx>=D.length-1)? 0 : goal.idx+1;
location = '#'+D.id;
}
</script>
Not tested !