HTML5 输入类型='数字' - 区分焦点和值更改鼠标点击

发布于 2024-12-02 05:15:34 字数 1414 浏览 0 评论 0原文

在 HTML5 INPUT type='number' 中,用户可以通过单击 INPUT 框一部分的向上/向下箭头来更改值。用户还可以单击该框以获得焦点或编辑其内容。

有没有简单的方法可以在 click 触发器中区分这两个活动?


来自 @cvsguimaraes 的回答,它更好地证明了该理论。

使用他的方法,这是我完成的(?)版本。目的:确保在使用 +/- 更改数据时调用常规 change 触发器。

// input/mouseup triggers to call change from +/- mouse clicks
// want to wait 500ms before calling change in case successive clicks
render.inputCh = false;
render.inputCt = 0;
render.inputFn  = function(e) {
    render.inputCh = true;
}
render.mouseupFn  = function(e) {
    if( render.inputCh ) {
        render.inputCh = false;
        render.inputCt++;
        setTimeout( next, 500 );
    }       
    function next() {
        render.inputCt--;
        if( render.inputCt ) return;
        var change = document.createEvent("Event");
        change.initEvent('change',true,true);
        e.target.dispatchEvent(change);
    }
}

// using input/mouseup triggers
document.getElementById('number').addListener('input',render.inputFn,true);
document.getElementById('number').addListener('mouseup',render.mouseuptFn,true);

// normal change trigger - will be called normally and via +/- mouse click
document.getElementById('number').addListener('change',changeFn,false);

在 chrome 上,到目前为止,它工作得非常完美除了,当您从 ITEM 上移除焦点时,change 触发器将再次启动。我通过使用低级别更改触发器解决了这个问题,如果之前的更改调用来自鼠标按钮,则该触发器会停止传播。

In the HTML5 INPUT type='number' the user can change the value by clicking on up/down arrows that are part of the INPUT box. The user might also click in the box for focus or for editing its contents.

Is there any easy way to distinguish between these two activities in the click trigger?


from @cvsguimaraes answer, which better demonstrates the theory.

using his methodology, here is my finished(?) version. the purpose: make sure regular change triggers are called when using +/- to change data.

// input/mouseup triggers to call change from +/- mouse clicks
// want to wait 500ms before calling change in case successive clicks
render.inputCh = false;
render.inputCt = 0;
render.inputFn  = function(e) {
    render.inputCh = true;
}
render.mouseupFn  = function(e) {
    if( render.inputCh ) {
        render.inputCh = false;
        render.inputCt++;
        setTimeout( next, 500 );
    }       
    function next() {
        render.inputCt--;
        if( render.inputCt ) return;
        var change = document.createEvent("Event");
        change.initEvent('change',true,true);
        e.target.dispatchEvent(change);
    }
}

// using input/mouseup triggers
document.getElementById('number').addListener('input',render.inputFn,true);
document.getElementById('number').addListener('mouseup',render.mouseuptFn,true);

// normal change trigger - will be called normally and via +/- mouse click
document.getElementById('number').addListener('change',changeFn,false);

on chrome it's working flawlessly so far except that when you remove focus from the ITEM the change trigger will kick in again. I solved this by having a low level change trigger that stops propagation if the previous change call was from the mouseup.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苯莒 2024-12-09 05:15:34

这里演示了如何捕获和分析哪些事件更改了输入以及按什么顺序更改。

var input = document.getElementById('number'),
    events = [
        "click",
        "mousedown",
        "mouseup",
        "focus",
        "change",
        "input",
        "keydown",
        "keypress",
        "keyup"
    ];
events.forEach(function(ev) {
    input.addEventListener(ev, function() {
        console.log(ev, ' => ', input.value);
    }, false);
});
<input type="number" id="number" />

Here is a demo of how you can capture and analyze which events change the input and in what order.

var input = document.getElementById('number'),
    events = [
        "click",
        "mousedown",
        "mouseup",
        "focus",
        "change",
        "input",
        "keydown",
        "keypress",
        "keyup"
    ];
events.forEach(function(ev) {
    input.addEventListener(ev, function() {
        console.log(ev, ' => ', input.value);
    }, false);
});
<input type="number" id="number" />

忆梦 2024-12-09 05:15:34

当用户通过单击向上/向下箭头更改值时,会在 mousedownmouseup 之间方便地触发 input 事件。

const input = document.getElementById('age')
const on = (ev, fn) => input.addEventListener(ev, fn)

const status = document.getElementById('status')
const write = (...txt) => status.innerText = txt.join()

let lastEv = 'none';
on('keydown', () => lastEv = 'keydown')
on('mousedown', () => lastEv = 'mousedown')

on('input', () => {
  if (lastEv === 'mousedown') write('changed from mouse', input.value);
  if (lastEv === 'keydown') write('changed from keyboard', input.value)
});
<input id="age" type="number" value=30> <br>
<div id="status"></div>

When the user change the value by clicking on up/down arrows the input event is triggered conveniently between mousedown and mouseup.

const input = document.getElementById('age')
const on = (ev, fn) => input.addEventListener(ev, fn)

const status = document.getElementById('status')
const write = (...txt) => status.innerText = txt.join()

let lastEv = 'none';
on('keydown', () => lastEv = 'keydown')
on('mousedown', () => lastEv = 'mousedown')

on('input', () => {
  if (lastEv === 'mousedown') write('changed from mouse', input.value);
  if (lastEv === 'keydown') write('changed from keyboard', input.value)
});
<input id="age" type="number" value=30> <br>
<div id="status"></div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文