javascript Jquery - 检测输入之外的点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25162665/
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-28 04:05:08  来源:igfitidea点击:

Jquery - detect the click outside of input

javascriptjquery

提问by doniyor

<input id="ok" /> 

$('.hor-minimalist-a').on('blur','#ok',function(){

});

with this code above, i can handle events which fire If i leavethe input field.

使用上面的代码,如果我离开输入字段,我可以处理触发的事件。

How can I detect if someone clicks outside of inputfield withoutcoming to input. I mean the user never came to this input field before, so there was no focus at all.

我如何检测是否有人在没有输入的情况下点击了输入字段。我的意思是用户以前从未来到过这个输入字段,所以根本没有焦点。

$('.hor-minimalist-a').on('?','#ok',function(){
  ?
});

回答by adeneo

$(document).on('click', function(e) {
    if ( e.target.id != 'ok' ) {
        // you clicked something else
    }
});

That would capture any click except on the input, but why would you need it ?

这将捕获除输入之外的任何点击,但您为什么需要它?

To capture only clicks on .hor-minimalist-aand not the entire document, just replace documentwith that selector in the code above.

要仅捕获点击.hor-minimalist-a而不是整个文档,只需document在上面的代码中替换为该选择器即可。

回答by tymeJV

How about:

怎么样:

$('.hor-minimalist-a').on('click',':not(#ok)',function(){

That'll register click events within .hor-minimalist-abut outside the input #ok

这将在内部.hor-minimalist-a但外部注册点击事件input #ok