我如何知道哪个元素具有焦点?

发布于 2024-10-31 07:10:26 字数 38 浏览 6 评论 0原文

我如何知道哪个元素拥有焦点,或者如何确定某个元素是否获得焦点?

How do I know which element has the focus, or how to determine whether the focus of a particular element?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这个俗人 2024-11-07 07:10:26

我不知道如何直接查询此类信息。我想你可以遍历 DOM 并手动检查它。如果是这种情况,您应该看看这个< /a>.

也许您想要做的事情可以通过在感兴趣的特定元素获得焦点时处理事件来实现。以下是在 jQuery 中如何做到这一点。

$('<selector>').focus(function() {
  // Do something here
});

只是为了咯咯笑,这是一种可以使用 focus 方法来跟踪哪个元素最后收到它的方法。我强烈建议不要使用此代码。

var gotDaFocus;
$('*').focus(function() {
  gotDaFocus = this;
}); 

I don't know of anyway to query for this kind of information directly. I suppose you could traverse the DOM and check for it manually. If that's the case you should look at this.

Perhaps what you are trying to do can be achieved by handling an event when specific elements of interest gain focus. Here is how you would do that in jQuery.

$('<selector>').focus(function() {
  // Do something here
});

Just for giggles, here is one way you could use the focus method to track which element recieved it last. I highly recommend against using this code.

var gotDaFocus;
$('*').focus(function() {
  gotDaFocus = this;
}); 
陌上芳菲 2024-11-07 07:10:26

恐怕尚未实施。
你可以这样做

Shoes.app do
  @focused = para ""
  stack do
    @label1 = edit_line :text => 1
    click do
      @focused.replace @label1.text
    end
  end
  stack do
    @label2 = edit_line :text => 2
    click do
      @focused.replace @label2.text
    end
  end
end

,但 Windows 中的问题是控件窃取了 Shoes 的所有事件。
在绿鞋中它会起作用。如果您在 Windows 中,单击 edit_line 的右侧仍然可以看到结果。当然,这只是当您用鼠标单击时,您应该检查 keypress 以获取键盘事件。
格茨

not yet implemented yet i'm afraid.
You could do something like this

Shoes.app do
  @focused = para ""
  stack do
    @label1 = edit_line :text => 1
    click do
      @focused.replace @label1.text
    end
  end
  stack do
    @label2 = edit_line :text => 2
    click do
      @focused.replace @label2.text
    end
  end
end

But the problem in windows is that the controls steal all the event from Shoes.
In Green Shoes it would work. If you'r in windows you can still see the result if you click just to the right of the edit_line. Of course, this is only when you click with the mouse, you should check keypress tot get the keyboard events also.
Grtz

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文