javascript 检测Javascript事件类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7354766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:50:33  来源:igfitidea点击:

Detect Javascript event type

javascripteventsbackbone.js

提问by William Sham

There is a function OPEN in my javascript which is called when the user either blur (lose focus on the input field) or hit Enter.

我的 javascript 中有一个 OPEN 函数,当用户模糊(失去对输入字段的关注)或按 Enter 键时会调用该函数。

Then within OPEN(), depending on whether it was triggered by blur or keypress, it leads to two different other functions.

然后在 OPEN() 中,根据它是由模糊还是按键触发,它会导致两个不同的其他功能。

For the Keypress, I did it like this.

对于 Keypress,我是这样做的。

        if (e.keyCode==13) ENTER_FX();

How do you do this for BLUR

你如何为 BLUR 做到这一点

Thank you

谢谢

UPDATE:

更新:

I found that it should be e.type=="focusout"

我发现它应该是 e.type=="focusout"

So is focusout the right word instead of blur?

那么 focusout 而不是 blur 是正确的词吗?

回答by jondavidjohn

WORKING JSFIDDLE EXAMPLE

工作 JSFIDDLE 示例

e.type

gives you this information

给你这个信息

function OPEN(e) {
    if (e.type !== "blur") {
        if (e.keyCode === 13) {
            ENTER_FX();
        }
    }
    else {
        ENTER_FX();
    }
}

回答by Marc B

e.typeshould probably say 'blur' in that case.

e.type在这种情况下,可能应该说“模糊”。

回答by Digital Plane

Try if(e.type == "blur") /*code here*/

尝试 if(e.type == "blur") /*code here*/

event.typereference

event.type参考