javascript - 如何检查焦点是否离开一组元素

标签 javascript jquery html

我想要一个包含 4 个输入的表单,通常只显示第一个输入。当它获得焦点时,其他 3 个输入变得可见。

如果焦点离开表单(用户在任何输入之外单击/点击/选项卡),则输入应隐藏。

我考虑过在表单或输入字段的 blur 事件上使用 document.activeElement 来检查输入丢失后焦点的去向。但是,blur 事件内的 document.activeElement 似乎总是返回 body,而不是实际要接收它的元素。

这就是我得到的:

// whenever an input loses focus:
//   if the focus is still inside the form,
//      keep the inputs visible
//   if focus left the form
//      hide the inputs, leaving only the first visible
$('#new-journal input').blur(function (e) {
    var $active = $(document.activeElement);
    if ($active.closest('#new-journal').length === 0) {
        $('#new-journal input:gt(0)').addClass('hide');
    }
});

...考虑到 #new-journal 是表单元素的 ID。

// whenever the user clicks/focuses the "Add a journal" input, 
// the other inputs inside the form appear
$('#name').focus(function(e) {
  $('#new-journal input:gt(0)').removeClass('hide');
});

// whenever an input loses focus:
//   if the focus is still inside the form,
//      keep the inputs visible
//   if focus left the form
//      hide the inputs, leaving only the first visible
$('#new-journal input').blur(function(e) {
  var $active = $(document.activeElement);
  if ($active.closest('#new-journal').length === 0) {
    $('#new-journal input:gt(0)').addClass('hide');
  }
});


// just logs who is the element with focus (document.activeElement)
$('*').on('focus blur', function(e) {
  var $history = $('#history'),
    current = document.activeElement,
    label = current.tagName + ' ' + current.id;
  $history.html($history.html() + label + '\n');
});
input[type=text] {
  padding: 4px 6px;
  display: block;
  margin: 0 0 5px;
  box-sizing: border-box;
}
.hide {
  display: none !important;
  height: 0;
  margin: 0;
  padding: 0;
}
pre:not(:empty) {
  border-radius: 2px;
  background-color: #eee;
  padding: 5px;
}
.col {
  float: left;
  width: 45%;
  margin: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col">
  <form id="new-journal">
    <input type="text" id="name" placeholder="Add a journal" />
    <input type="text" id="field1" placeholder="Sub title" class="hide" />
    <input type="text" id="field2" placeholder="Owner" class="hide" />
    <input type="text" id="field3" placeholder="Price" class="hide" />
  </form>
</div>
<div class="col">
  <p>document.activeElement:</p>
  <pre id="history"></pre>
</div>

除了使用 document.activeElement 之外,还有关于如何检测焦点是否仍在表单(任何可聚焦容器元素)内的想法吗?

<小时/>

更新: 我还尝试仅使用 CSS,并遵循以下规则:

#new-journal input:not(#name) {
    display: none;
}
#new-journal input:focus ~ input {
    display: block !important;
}

...但我得到了完全相同的结果。

最佳答案

您可以通过将 focus 函数应用于所有输入而不仅仅是第一个输入 (#name) 来解决该问题。

$('#new-journal input').focus(function (e) {
    $('#new-journal input:gt(0)').removeClass('hide');
});

这解决了问题并且不影响初始布局。您可以在这里看到它的工作原理:http://jsfiddle.net/vyd5dje6/ .

关于javascript - 如何检查焦点是否离开一组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28839723/

相关文章:

css - DIV 锚定到父 div 的顶部和底部

html - 将一个 div 置于另一个 div 之上

javascript - $(window).scroll() 在页面加载时触发

jquery - 克隆的 jQuery 可拖动不能自行放置

javascript - 为什么Ajax表单提交两次?

jquery - 如何使用命名空间创建 jQuery 元素方法

html - 为 IE8 上的表中的最后一个 td 提供宽度

javascript - 固定导航栏隐藏网页内容和 JS 不工作

Javascript继承封装,高效完成

JavaScript 和未定义的值