【问题标题】:JS backspace keypress event triggered sometimes, sometimes notJS退格按键事件有时会触发,有时不会
【发布时间】:2015-01-26 14:10:55
【问题描述】:

我有这段代码:

container.unbind('keypress').bind('keypress',function (e) {

            var code = e.which,
                charac = String.fromCharCode(code),
                totalChar = $(this).text().replace(/\s+/g, ''),
                diff,
                previousChar = publisher.typeHistory.length > 0 ? publisher.typeHistory[publisher.typeHistory.length-1]:''
                previousCharCode = previousChar.charCodeAt(0);

            if(e.keyCode === undefined){ // backspace
                getCharacterDiff();
                console.log(totalChar.length-1);
                if(totalChar.length-1 === 0){
                    publisher.typeHistory = [];
                }
            }  

            console.log(previousChar);
            if(isCharacterKeyPress(code)){
                if (charac.toUpperCase() != charac.toLowerCase() ) { // Is a char or space


                    if(charac === charac.toUpperCase()){ // Is a char upper ?

                        if(previousChar === ' '){
                            console.log('upper + previous == space ', publisher.typeHistory);
                        }else {
                            publisher.typeHistory = [];
                        }

                        push = true;
                    }
                    else if (charac === charac.toLowerCase()){
                        if(previousCharCode === 32){
                            publisher.typeHistory = [];

                        }
                        if(hasUpperCase(publisher.typeHistory.join(''))){
                            push = true;
                        }
                        else {
                            push = false;
                        }

                    }
                    else {
                        push = false;
                    }


                }
                else if (code === 32){
                    console.log('space');
                    console.log(previousCharCode);
                    if (previousCharCode === 32){
                        push = false;
                        publisher.typeHistory = [];
                    }

                    // else if(previousChar === 32) { // is the spacebar ?
                    //  console.log(previousChar);

                    // }
                }
                else { // = it's a number / , / ., etc..
                        console.log('not chara or space');
                        push = false;
                        publisher.typeHistory = [];



                }


                if (push) {
                    publisher.typeHistory.push(charac);
                }

            }

            console.log(publisher.typeHistory);
            previousTotal = publisher.typeHistory.join('');


            if (publisher.typeHistory.length > 2) {
                exp = publisher.typeHistory.join('');
                publisher.getResults(service,type,exp);
            }
            else {
                if(publisher.typeHistory.length === 0){
                    publisher.typeHistory = [];

                }
                publisher.hideResults();
            }

    });

通过这个,我可以处理在具有contenteditable=true 的元素中键入了哪些键,然后将其发送到向 PHP 脚本发出请求的方法。

这里,container 可以是 contenteditable=true 的任何元素。

问题是:我使用e.keyCode === undefined 来检查用户是否输入了退格,所以我可以更新请求。但e.keyCode === undefined 仅适用于单个元素,而不适用于其他元素(另一方面,keypress 事件未触发)。

所以我不知道这是从哪里来的:

  • 我知道 keypress 事件无法检测退格键按下(但是,在这种情况下,使用此代码,它可以)
  • 可以是container的anay元素之间的HTML / JS没有区别
  • 我不能使用 keyup / keydown,因为它不区分大小写

我错过了什么?

【问题讨论】:

  • 我知道 keypress 事件不应该在退格键上触发。你有支持这个说法的来源吗?
  • 好吧,我说的很混乱:我的意思是“按下退格键时不调用按键事件”。

标签: javascript jquery


【解决方案1】:

来自 keypress event: 上的 MDN

当一个键被按下并且该键被触发时,keypress 事件被触发 通常会产生一个字符值

因此您必须对某些键使用keyupkeydown 事件,例如转义或退格,因为它们不会产生字符。

退格键的键码是8,因此您的代码可能如下所示:

container.unbind('keyup').bind('keyup',function (e) {

    ...

    if(e.keyCode === 8){ // backspace
        getCharacterDiff();
        console.log(totalChar.length-1);
        if(totalChar.length-1 === 0){
            publisher.typeHistory = [];
        }
    }  

    ...

});

【讨论】:

  • 感谢您的回答,完成了这项工作。尽管如此,我不得不使用另一个更符合我需要的事件“DOMSubtreeModified”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多