HTML5 输入类型='数字' - 区分焦点和值更改鼠标点击
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里演示了如何捕获和分析哪些事件更改了输入以及按什么顺序更改。
Here is a demo of how you can capture and analyze which events change the input and in what order.
当用户通过单击向上/向下箭头更改值时,会在
mousedown
和mouseup
之间方便地触发input
事件。When the user change the value by clicking on up/down arrows the
input
event is triggered conveniently betweenmousedown
andmouseup
.