Detect Javascript event type

让人想犯罪 __ 提交于 2021-02-16 14:49:32

问题


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.

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

For the Keypress, I did it like this.

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

How do you do this for BLUR

Thank you

UPDATE:

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

So is focusout the right word instead of blur?


回答1:


WORKING JSFIDDLE EXAMPLE

e.type

gives you this information

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



回答2:


e.type should probably say 'blur' in that case.




回答3:


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

event.type reference



来源:https://stackoverflow.com/questions/7354766/detect-javascript-event-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!