如何实现按住按钮javascript?

cou*_*hua 6 javascript forms button

我是一个完整的新手,正在寻找有关实现javascript的说明.我试图用按钮和文本字段替换YUI滑块.我试图实现按钮,当按下时,将继续使文本字段增加,优选地以更快和更快的速率增加.(http://www.blackbird502.com/white.htm)我在头部的java标签中有这个:

function holdit(btn, action, start, speedup) {
var t;

var repeat = function () {
    action();
    t = setTimeout(repeat, start);
    start = start / speedup;
}

btn.mousedown = function() {
    repeat();
}

btn.mouseup = function () {
    clearTimeout(t);
}

/* to use */
holdit(btn, function () { }, 1000, 2); 
/* x..1000ms..x..500ms..x..250ms..x */
Run Code Online (Sandbox Code Playgroud)

我不知道如何实施印刷机并将其固定在机身内:

<form><input type=button value="UP"  class="btn" onClick="javascript:this.form.amount.value++;"><br /><input type=text name=amount value=5 class="text"><br /> <input type=button value="DOWN"  class="btn" onClick="javascript:this.form.amount.value--;" ></form>
Run Code Online (Sandbox Code Playgroud)

可能吗?谢谢.

s4y*_*s4y 5

这段代码应该做你想要的一切; 它基于tj111的例子非常宽松.我试图让它尽可能重用,并且它不需要与HTML混合使用JavaScript.

您需要在按钮(btnUPbtnDOWN)和文本字段(amount)中添加ID .您可以在window.onload语句中更改这些ID .

// This function creates a closure and puts a mousedown handler on the element specified in the "button" parameter.
function makeButtonIncrement(button, action, target, initialDelay, multiplier){
    var holdTimer, changeValue, timerIsRunning = false, delay = initialDelay;
    changeValue = function(){
        if(action == "add" && target.value < 1000)
            target.value++;
        else if(action == "subtract" && target.value > 0)
            target.value--;
        holdTimer = setTimeout(changeValue, delay);
        if(delay > 20) delay = delay * multiplier;
        if(!timerIsRunning){
            // When the function is first called, it puts an onmouseup handler on the whole document 
            // that stops the process when the mouse is released. This is important if the user moves
            // the cursor off of the button.
            document.onmouseup = function(){
                clearTimeout(holdTimer);
                document.onmouseup = null;
                timerIsRunning = false;
                delay = initialDelay;
            }
            timerIsRunning = true;
        }
    }
    button.onmousedown = changeValue;
}

//should only be called after the window/DOM has been loaded
window.onload = function() {
    makeButtonIncrement(document.getElementById('btnUP'), "add", document.getElementById('amount'), 500, 0.7);
    makeButtonIncrement(document.getElementById('btnDOWN'), "subtract", document.getElementById('amount'), 500, 0.7);
}
Run Code Online (Sandbox Code Playgroud)