简体   繁体   English

jQuery如何拆分模糊并输入密钥

[英]Jquery how to split blur and enter key

I want to split blur and enter key functions. 我想分割模糊并输入关键功能。 So I mean that I want jquery to do another function on blur and another on enter key. 所以我的意思是我希望jquery在模糊上执行另一个功能,在enter键上执行另一个功能。 If enter key was clicked then blur mustn't work, so blur function mustn't execute. 如果单击回车键,则模糊不能起作用,因此不能执行模糊功能。 This is my jquery code : 这是我的jQuery代码:

$(document).ready(function(){   
    $("#comment_textarea").on("keypress blur", function(e) {

        if(e.type == "keypress" & e.which == 13){
            alert("type: "+e.type+"||which: "+e.which);
        }
        else if(e.type != "keypress" ){
            alert("type: "+e.type+"||which: "+e.keycode);
        }
    });
})

This code alerts two times. 此代码会发出两次警报。 First is blur and second is enter click. 首先是模糊,其次是输入click。 Have anyone got any ideas. 有没有人有任何想法。

Thanks. 谢谢。

由于您显示警报,因此文本区域不再聚焦,然后将触发模糊事件。

$(function () {
    $("#comment_textarea").on("keydown", function (e) {
        if (e.keyCode == 13) {
            // do your Enter key stuff
            e.preventDefault();
        }
    });
    $("#comment_textarea").on("blur", function (e) {
        // handle the blur
    });
});

Trying to double up probably isn't the best way. 试图加倍并不是最好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM