如何在 JavaScript 中获取先前聚焦的元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7329141/
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
How do I get the previously focused element in JavaScript?
提问by HashimR
I have an HTML form with multiple text inputs. I want to clear the element that had the focus immediately priorto when the 'Clear' button is pressed. How do I get that element in JavaScript?
我有一个带有多个文本输入的 HTML 表单。我想在按下“清除”按钮之前立即清除具有焦点的元素。如何在 JavaScript 中获取该元素?
Note: Reworded to take comments into account.
注意:重新措辞以考虑评论。
采纳答案by genesis
When you click "clear" button, only element focused is "clear" button. You'll have to workaround it. (trigger onblur event)
当您单击“清除”按钮时,只有聚焦的元素是“清除”按钮。你必须解决它。(触发 onblur 事件)
回答by laradev
Create a global variable for storing the current focused element's id,
创建一个全局变量来存储当前焦点元素的 id,
var cur_id;
call one function for onblur
of each of elements and pass id
为onblur
每个元素调用一个函数并传递 id
<input type="text" id="name" name="name" onBlur="setId(this.id)">
and write the set the id to global variable from that function
并将该 id 设置为该函数的全局变量
function setId(id) {
cur_id = id;
}
and write a function for onclick of clear button, like this
并为清除按钮的onclick编写一个函数,就像这样
function clear() {
document.getElementById(cur_id).value = "";
}
回答by shesek
var focused, inputs = document.getElementsByTagName('input');
for (var i=0, input; i<inputs.length && (input = inputs[i]); i++) {
if (input.type === 'text') {
input.addEventListener('focus', function(){
focused = this;
});
}
}
Or in jQuery:
var focused; $('input:text').focus(function(){focused = this;});
或者在 jQuery 中:
var focused; $('input:text').focus(function(){focused = this;});
Then, when you want to clear the focused element, focused.value='';
然后,当你想清除焦点元素时, focused.value='';
回答by Tim Down
The simplest way is to store a reference to document.activeElement
within the mousedown
event on the button, although this is a mouse-centric approach and won't work for button "clicks" from keyboards and other devices.
最简单的方法是在按钮上document.activeElement
的mousedown
事件中存储一个引用,尽管这是一种以鼠标为中心的方法,并且不适用于来自键盘和其他设备的按钮“点击”。
Live demo: http://jsfiddle.net/tEP6w/
现场演示:http: //jsfiddle.net/tEP6w/
Code:
代码:
var clearButton = document.getElementById("clear");
var previousActiveElement = null;
clearButton.onmousedown = function() {
previousActiveElement = document.activeElement;
};
clearButton.onclick = function() {
if (previousActiveElement && previousActiveElement != this) {
previousActiveElement.value = "";
}
};
A better approach would be to store a reference to document.activeElement
as the button is about to receive the focus. However, this is a little tricky to achieve nicely in all browsers.
更好的方法是document.activeElement
在按钮即将获得焦点时存储对的引用。但是,在所有浏览器中都很好地实现这一点有点棘手。
回答by JW8
As Genesis mentions, the clear button will have the focus when clicked. However, you may be able to work around this by having an onBlur event for your form that would store the id of the previously touched element in a variable or hidden form input.
正如 Genesis 所提到的,点击清除按钮时将获得焦点。但是,您可以通过为您的表单设置一个 onBlur 事件来解决这个问题,该事件会将先前触摸的元素的 id 存储在变量或隐藏表单输入中。
回答by RickL
Working on the ideas above, and if you're working with a form, you can define a hidden input and assign it a value of the last focused input's id:
处理上述想法,如果您正在使用表单,您可以定义一个隐藏输入并为其分配最后一个焦点输入的 id 值:
<input type="hidden" id="focused_element" value="">
$('input:text, textarea').focus(function() {
$('#focused_element').val($(this).attr('id'));
});
and then pick up the value at the appropriate later time:
然后在适当的稍后时间取值:
var focused = $('#focused_element').val();