一个输入按键运行事件处理程序两次 [英] one enter keypress runs event handler twice

查看:76
本文介绍了一个输入按键运行事件处理程序两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么一次通过此代码进入按键运行(内部)两次?

我尝试过按键,keydown以及event.preventdefault()无效。



 $( document )。keydown( function (e){
if (e.which == 13 ){
alert( 0
var focado = document .getElementById( document .activeElement.id),fim = focado .id.substring( 1 ),
inicio = focado.id.substring( 0 1 ),
fimseg = fim,
anterior = inicio +(fim - 1 ),

alert( 1


alert( focado + focado.id + fim + fim + inicio + inicio + fimseg + fimseg + anterior + anterior);
}
});





并且只有在给出这3条消息两次之后它才会继续剩下的代码我修剪了并且没有显示。



谢谢

解决方案

document )。keydown( function (e){
if (e.which == 13 ){
alert( 0
var focado = document .getElementById( document .activeElement.id),fim = focado.id.substring( 1 ),
inicio = focado.id.substring( 0 1 ),
fimseg = fim,
anterior = inicio +(fim - 1 ),

alert( 1


alert( focado + focado.id + fim + fim + inicio + inicio + fimseg + fimseg + anterior + anterior);
}
});





并且只有在给出这3条消息两次之后它才会继续剩下的代码我修剪了并且没有显示。



谢谢


请使用代码

它可以帮助您防止立即两次执行事件

 


document ).keydown( function (e){
e.stopImmediatePropagation();
......
......





保持其余的处理程序不被执行,并防止事件冒出DOM树。


Why does one enter keypress runs (inside) through this code twice?
I've tried keypress, keydown and also event.preventdefault() to no avail.

    $(document).keydown(function (e) {
        if (e.which == 13) {
           alert("0")
            var focado = document.getElementById(document.activeElement.id), fim = focado.id.substring(1),
                inicio = focado.id.substring(0, 1),
                fimseg = fim,
                anterior = inicio + (fim - 1),
                
            alert("1")
            
        
            alert("focado " + focado.id + " fim " + fim + " inicio " + inicio + " fimseg " + fimseg + " anterior " + anterior);
        }
});



and only after giving this 3 messages twice it would continue to the remaining code I trimmed and isn't shown.

thanks

解决方案

(document).keydown(function (e) { if (e.which == 13) { alert("0") var focado = document.getElementById(document.activeElement.id), fim = focado.id.substring(1), inicio = focado.id.substring(0, 1), fimseg = fim, anterior = inicio + (fim - 1), alert("1") alert("focado " + focado.id + " fim " + fim + " inicio " + inicio + " fimseg " + fimseg + " anterior " + anterior); } });



and only after giving this 3 messages twice it would continue to the remaining code I trimmed and isn't shown.

thanks


Please use the code
It can helps you to prevent immediate twice execution of the event


(document).keydown(function (e) { e.stopImmediatePropagation(); ...... ......



Keeps the rest of the handlers from being executed and prevents the event from bubbling up the DOM tree.


这篇关于一个输入按键运行事件处理程序两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆