如果单击div或将其聚焦,则jQuery模糊不应该触发 [英] jQuery blur should not fire if div is clicked/focused

查看:122
本文介绍了如果单击div或将其聚焦,则jQuery模糊不应该触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有模糊事件的标准文本输入.单击文档中的任何内容(输入AND <div id='show-anyways'></div>除外)时,模糊事件应触发.

I have a standard text input that has a blur event. The blur event should fire when anything in the document is clicked (other than the input AND <div id='show-anyways'></div>).

我的问题是我不知道如何添加<div id='show-anyways'></div>来抵消模糊事件功能.

My problem is that I don't know how to add the <div id='show-anyways'></div> to negate the blur event function.

希望我对自己的解释足够好,我知道这有点奇怪.如果您需要更多信息,请告诉我.

Hopefully I have explained myself well enough, I know it's kinda a weird situation. Please let me know if you need more info.

推荐答案

使用事件目标确定单击的内容.如果元素是DIV或输入,则以下将跟踪对文档的所有单击,而不会触发blur().

Use the event target to determine what is clicked. Following will track all clicks on document and not trigger blur() if element is the DIV or the Input.

var $input=$( '#MY-Input-ID');

$(document).click(function(evt){
   var $tgt=$(evt.target);
   if( !$tgt.is( '#show-anyways') && !$tgt.is($input) ){
         $input.blur()
    }
})

它不会在设置了preventPropogation的元素上触发.

It will not trigger on elements that have any preventPropogation set up.

这篇关于如果单击div或将其聚焦,则jQuery模糊不应该触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆