document.activeElement的jQuery替代方案 [英] jQuery alternative for document.activeElement
问题描述
我想要做的是弄清楚用户何时使用INPUT或TEXTAREA元素并将变量标志设置为true ...并在用户不再使用它之后立即将该标志设置为false(即他们点击了INPUT / TEXTAREA元素。
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).
我使用jQuery的docuemnt.ready函数将onclick属性添加到我的body元素并将其分配给我的getActive()函数。
I used jQuery's docuemnt.ready function to add the onclick attribute to my body element and assign it to my getActive() function.
getActive()函数的代码如下:
The code for the getActive() function is as follows:
function getActive()
{
activeObj = document.activeElement;
var inFocus = false;
if (activeObj.tagName == "INPUT" || activeObj.tagName == "TEXTAREA")
{
inFocus = true;
}
}
我真的很喜欢用jQuery框架保留项目,但是不能似乎找到了一种使用JUST jQuery语法完成相同逻辑的方法。
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.
推荐答案
您需要焦点和模糊事件处理程序。例如......
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;
});
我很确定逗号会让你输入OR textarea,但你明白了没有成功
I'm pretty sure that a comma will get you input OR textarea, but you get the idea if that doesn't pan out
这篇关于document.activeElement的jQuery替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!