Si vous ne se soucient pas de soutenir Opera, c'est facile:
textbox.onkeydown = function(e)
{
if (e.keyCode == 38)
{
incrementTextBox();
}
}
Cependant, Opera ne se déclenche pas keydown
pour les répétitions clés ... vous devrez imiter que en appelant à incrementTextBox()
un intervalle, et l'arrêt lorsque la clé est levée. Je l'ai testé cela dans WebKit (Chrome 6.0), FF3, Opera 10.6, IE7, IE8, IE9, même IE Quirks:
var textbox = null;
window.onload = function()
{
var timeoutId = null;
var intervalId = null;
var incrementRepeatStarted = false;
function startIncrementKeyRepeat()
{
timeoutId = window.setTimeout(function()
{
intervalId = window.setInterval(incrementTextBox, 50);
}, 300);
}
function abortIncrementKeyRepeat()
{
window.clearTimeout(timeoutId);
window.clearInterval(intervalId);
timeoutId = null;
intervalId = null;
}
function endIncrementKeyRepeat()
{
abortIncrementKeyRepeat();
incrementRepeatStarted = false;
}
textbox = document.getElementById("incrementer");
textbox.onkeydown = function(e)
{
e = e || window.event;
if (e.keyCode == 38)
{
if (!incrementRepeatStarted)
{
startIncrementKeyRepeat();
incrementRepeatStarted = true;
}
else if (timeoutId || intervalId)
{
abortIncrementKeyRepeat();
}
incrementTextBox();
}
else if (incrementRepeatStarted)
{
endIncrementKeyRepeat();
}
}
textbox.onkeyup = endIncrementKeyRepeat;
}
function incrementTextBox()
{
var val = parseInt(textbox.value) || 0;
val++;
textbox.value = val;
}
'onkeydown' sera en effet le travail, et il ne sera pas « massivement » rapide dans tous les cas - clés ne feront que répéter à la taux défini dans les paramètres du système d'exploitation. – casablanca
Eh bien, super! – Jas