简体   繁体   English

我可以检测到焦点的来源吗? (JavaScript,jQuery)

[英]Can I detect the source of a focus? (Javascript, jQuery)

Quick question - is it possible to detect whether a focus has come from a mouse click or a tab from a focus event? 快速提问-是否有可能检测到焦点是来自单击鼠标还是来自焦点事件的选项卡?

I guess if there isn't, I'll have to use a click handle on the same element to determine the source, but I'd prefer a way from the focus event. 我想如果没有的话,我将不得不在同一元素上使用单击手柄来确定源,但是我更喜欢焦点事件中的一种方法。

Thanks 谢谢

Gausie 高斯

May not work 100% but if there isn't a direct way then can't you just use Mouseover and detect it? 可能无法100%起作用,但是如果没有直接方法,那么您是否只能使用Mouseover进行检测? The person will have bring the mouse over the control to select it (?) 该人将鼠标移至控件上进行选择(?)

I'm quite confident that a focus event does not trac the way the focus has been ordered (window.focus, key, click...). 我非常有信心,焦点事件不会追踪焦点的排序方式(window.focus,key,click ...)。

But in the case of a click, you can detect the mouse click. 但是在单击的情况下,您可以检测到鼠标单击。 You can also detect keyboard event (more on that http://www.quirksmode.org/js/keys.html ). 您还可以检测键盘事件(有关http://www.quirksmode.org/js/keys.html的更多信息 )。

What about using mouse position? 如何使用鼠标位置?

In the event handler, compare the current mouse position with the area of your control. 在事件处理程序中,将当前鼠标位置与控件区域进行比较。 If the coordinates fall within the area of the control don't scroll. 如果坐标落在控件区域内,请不要滚动。

This is not a perfect solution of course, since they could hover the element, and then tab to it without clicking. 当然,这不是一个完美的解决方案,因为他们可以将元素悬停,然后在不单击的情况下跳至该元素。 But since you are attempting to reduce any disorientation, this might actually be good side effect. 但是,由于您正在尝试减少任何迷失方向,因此这实际上可能是很好的副作用。

When working with mouse position, I like to use the script from quirksmode.org . 使用鼠标位置时,我喜欢使用quirksmode.org中的脚本。 I think Jquery may provide some functionality for it too. 我认为Jquery可能也提供一些功能。

Try getting the keyCode - here's a link to an article that goes into detail on how. 尝试获取keyCode-这是指向详细介绍操作方法的文章的链接。 The comments at the bottom may be useful as well. 底部的注释也可能有用。

http://www.geekpedia.com/tutorial138_Get-key-press-event-using-JavaScript.html http://www.geekpedia.com/tutorial138_Get-key-press-event-using-JavaScript.html

Here's the code from the article. 这是文章中的代码。 It doesn't show it, but the keyCode for tab is 9. 它没有显示,但是tab的keyCode是9。

<script type="text/javascript">

document.onkeyup = KeyCheck;       

function KeyCheck(e)

{

   var KeyID = (window.event) ? event.keyCode : e.keyCode;


   switch(KeyID)

   {

      case 16:

      document.Form1.KeyName.value = "Shift";

      break; 

      case 17:

      document.Form1.KeyName.value = "Ctrl";

      break;

      case 18:

      document.Form1.KeyName.value = "Alt";

      break;

      case 19:

      document.Form1.KeyName.value = "Pause";

      break;

      case 37:

      document.Form1.KeyName.value = "Arrow Left";

      break;

      case 38:

      document.Form1.KeyName.value = "Arrow Up";

      break;

      case 39:

      document.Form1.KeyName.value = "Arrow Right";

      break;

      case 40:

      document.Form1.KeyName.value = "Arrow Down";

      break;
   }

}
</script>

If anyone's interested, here's how I did it: 如果有人感兴趣,这就是我的做法:

$('input').focus(function(){
    $this = $(this);
    $this.parents('tr').css('background-color','yellow');   
    if($this.attr('click')!='true'){
        $('body').scrollTop($this.offset().top-($(window).height()/2));
    }
}).blur(function(){
    $(this).parents('tr').css('background-color','transparent');
}).mouseover(function(){
    $(this).attr('click','true');
}).mouseout(function(){
    $(this).removeAttr('click');
});

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

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