如何检查焦点是否离开了一组元素
How to check if the focus left a group of elements
我想要一个有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/.
相关文章:
- 创建一组元素JointJS
- 如何检查焦点是否离开了一组元素
- 是否可以加快向DOM添加一组元素的速度
- 使用javascript在一组元素上切换类
- 如何使用jQuery查找一组元素的父元素
- 为什么 .data() 适用于一组元素,但不适用于单个元素
- 如何从一组元素中获取 ID 并保存在数组中
- 使一组元素透明,但一个除外
- 当一组元素的所有元素都具有特定值时,如何隐藏表单元素
- 如何在一组元素上触发事件
- JQuery:用另一组元素替换一组元素的优雅方式
- 如何使用 jquery 隐藏下一组元素
- 使用 Raphael JS 一次缩放一组元素
- 循环遍历一组元素 jQuery
- 在没有循环的情况下测试一组元素之间的html5数据相等性
- 为一组元素逐个设置放置阴影的动画
- 当一组元素不总是在同一个父元素中时,如何找到下一个匹配的元素
- 如何在InDesign CS5 javascript中将一组元素锚定到特定的TextFrame
- 使用jQuery将一组元素中的每一个克隆到单独的目标元素中
- 使用jQuery计算一组元素的最大宽度