单击其他元素时模糊(不聚焦)某个元素 [英] Blur(unfocus) an element when other element is clicked

查看:62
本文介绍了单击其他元素时模糊(不聚焦)某个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

document.addEventListener("click", function (e) {
var filter = document.getElementById('filter');
  if (e.target !== filter[0]) {
    filter.blur();
  }
});

我的尝试^^

jQuery代码:

$(document).on('click', function(e) { 
  if (e.target !== $('#filter')[0]) 
  $('#filter').blur();
});

当单击除#filter以外的任何元素时,我想模糊(散焦)#filter

When any element except #filter is clicked I would like to blur (unfocus) #filter

推荐答案

您无需使用 [0] 取消引用 getElementById 的结果-结果已经是一个元素.

You don't need to dereference the result of getElementById with the [0] - the result is already a single element.

[0] 仅在jQuery中是必需的,因为即使只有一个匹配元素,jQuery结果也始终是伪数组.

The [0] is only necessary in jQuery because jQuery results are always pseudo-arrays even if there's only one matching element.

鉴于每次在页面上单击时都会调用此函数,请考虑将 filter 的定义移到该函数之外,这样您就不必在每次单击时都重新评估它(假设该元素是静态的.

Given this function is being called for every single click on the page, consider moving the definition of filter outside of the function so that you don't have to re-evaluate it for every click (assuming that the element is static).

这篇关于单击其他元素时模糊(不聚焦)某个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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