Javascript document.activeElement 的 jQuery 替代品
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3328320/
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
jQuery alternative for document.activeElement
提问by Brownbay
What I wanted to do is figure out whenever the user is engaged with an INPUT or TEXTAREA element and set a variable flag to true... and set that flag to false immediately after the user is no longer engaged with them (ie. they've clicked out of the INPUT/TEXTAREA elements).
我想要做的是弄清楚用户何时使用 INPUT 或 TEXTAREA 元素并将变量标志设置为 true...并在用户不再使用它们后立即将该标志设置为 false(即他们)我点击了 INPUT/TEXTAREA 元素)。
I used jQuery's docuemnt.ready function to add the onclick attribute to my body element and assign it to my getActive() function.
我使用 jQuery 的 docuemnt.ready 函数将 onclick 属性添加到我的 body 元素并将其分配给我的 getActive() 函数。
The code for the getActive() function is as follows:
getActive() 函数的代码如下:
function getActive()
{
activeObj = document.activeElement;
var inFocus = false;
if (activeObj.tagName == "INPUT" || activeObj.tagName == "TEXTAREA")
{
inFocus = true;
}
}
I'd really like to keep by project withing the jQuery framework, but can't seem to find a way of accomplishing the same logic above using JUST jQuery syntax.
我真的很想继续使用 jQuery 框架进行项目,但似乎无法找到一种使用 JUST jQuery 语法完成上述相同逻辑的方法。
采纳答案by Rob
You want the focus and blur event handlers. For example...
您需要焦点和模糊事件处理程序。例如...
var inFocus = false;
$('input, textarea').focus(function() {
inFocus = true;
});
$('input, textarea').blur(function() {
inFocus = false;
});
I'm pretty sure that a comma will get you input OR textarea, but you get the idea if that doesn't pan out
我很确定逗号会让你输入或文本区域,但如果没有成功,你就会明白
回答by user1933288
function getActive(){
return $(document.activeElement).is('input') || $(document.activeElement).is('textarea');
}
回答by Shital Shah
For the original question about figuring out if the currently focused element is any of those user input elements, below should work:
对于关于确定当前聚焦的元素是否是这些用户输入元素中的任何一个的原始问题,以下应该有效:
function isInputElementInFocus() {
return $(document.activeElement).is(":input");
}
Conceptually I don't like this approach for generic case where you are listening to global events (like key strocks) and trying to decide if these should be handled by your global handler or be ignored because it is meant for someone else. The reason I don't like it because it's not future safe and also who knows what else that someone canbe besides input elements.
从概念上讲,我不喜欢这种方法用于一般情况下您正在侦听全局事件(如按键)并试图决定这些事件是否应该由您的全局处理程序处理或被忽略,因为它是为其他人准备的。我不喜欢它的原因是因为它在未来不是安全的,而且谁知道除了输入元素之外还有什么人可以成为。
Another more robust but tricky to implement idea is to test if event is meant for an element that has tabIndex >= 0. The input elements have tabIndex === 0 set by default so it becomes more or less similar to above approach. You can easily check this by event.target.tabIndex >= 0without need to rely on document.activeElement.
另一个更健壮但难以实现的想法是测试事件是否针对 tabIndex >= 0 的元素。输入元素默认设置 tabIndex === 0,因此它或多或少类似于上述方法。您可以轻松地检查这一点,event.target.tabIndex >= 0而无需依赖document.activeElement.
The gotchas here (if you want to be generic) is that you also need to make sure that event.target element is neither in another branch in DOM hierarchy nor there is someone else between event.currentTarget and event.target that has tabIndex >= 0. You get the idea: This can become murky but I just thought to jot it down if someone else is in need of generic solution.
这里的问题(如果你想要通用)是你还需要确保 event.target 元素既不在 DOM 层次结构中的另一个分支中,也不在 event.currentTarget 和 event.target 之间有其他人有tabIndex >= 0. 你明白了:这可能会变得模糊,但我只是想如果其他人需要通用解决方案,就把它记下来。
回答by Soufiane Hassou
You can do something like this :
你可以这样做:
var focusItem = null;
$('input, textarea').focus( function() {
focusItem = this;
});

