javascript Js-jQuery - 检查元素是否聚焦或获取最后聚焦的元素类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16593403/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:15:50  来源:igfitidea点击:

Js-jQuery - check if element is focused or get last focused element class

javascriptjquerydomfocusdocument

提问by itsme

is it possible to check if an element is focused?

是否可以检查一个元素是否被聚焦?

i tryed:

我试过:

 <a href="#"></a>
    $('a').is(':focused');

but it won't work.

但它不会工作。

Also i would like to understand if is possible to get the last focused document element attr class:

另外我想了解是否有可能获得最后一个聚焦的文档元素 attr 类:

var last_focused_class = document.lastFocusedElement.class;

for example

例如

回答by tymeJV

You were close, to test for focus:

你很接近,测试焦点:

$('a').is(':focus');

Not sure about how to get the last focused element, you may have to design a hack for it...something like:

不确定如何获得最后一个焦点元素,您可能需要为它设计一个 hack ......类似于:

var lastFocusedClass;
$(document).on('focus', '*', function() {
    lastFocusedClass = $(this).attr("class");
});

回答by Anand Singh

In my case this works... $("*:focus")

在我的情况下,这有效...... $("*:focus")

You can get id,name,valueetc.And check if this is the element you are looking for.

你可以得到idnamevalue等二十多个国家检查,如果这是你正在寻找的元素。

var checkValue = $("*:focus").attr("...");