简体   繁体   English

如何从页面中找到焦点元素

[英]How can I find focus element from my page

I want to find which element is focused.For this I tried in the following way. 我想找到关注的元素。为此,我尝试了以下方法。

I have 2 textfields on my web page id's are field1,field2 我的网页ID上有2个文本字段,分别是field1,field2

var viewObj=Marionette.CompositeView.extend({
  events:{
      "blur #field1":"test",
      "blur #field2":"test"
   },
 test:function(){
   console.log(document.activeElement.id);
 }
});

what I want, 我想要的是,

If you move cursor from field1 to field2 prints field2 如果将光标从field1移到field2则会打印field2

If you move cursor from field2 to field1 prints field1 如果将光标从field2移到field1则会打印field1

If I click anywhere outside,then I want to print something else. 如果我在外面的任何地方单击,那么我想打印其他内容。

I know document.activeElement.id ,but it's not working. 我知道document.activeElement.id ,但是不起作用。

can anyone help me. 谁能帮我。

Thanks. 谢谢。

Use "document.activeElement" property . 使用“ document.activeElement”属性。

But Tis property only work for thoase field which accept keystrokes . 但是Tis属性仅适用于接受击键的区域。

If you are using JQuery: 如果您使用的是JQuery:

$("body").focusin(function(event) {
    alert(event.target.id);
});

It will alert you an ID of focused element. 它将提醒您ID集中的元素。

http://jsfiddle.net/2Lchy/9/ http://jsfiddle.net/2Lchy/9/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM