admin管理员组文章数量:1024615
Quick question - is it possible to detect whether a focus has e 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
Quick question - is it possible to detect whether a focus has e 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
Share Improve this question edited May 9, 2012 at 15:15 Ruan Mendes 92.3k31 gold badges159 silver badges221 bronze badges asked Dec 11, 2009 at 14:35 GausieGausie 4,3591 gold badge27 silver badges37 bronze badges 2- +1 because it's an interesting question. but i fail to see why you need that distinction. – just somebody Commented Dec 11, 2009 at 14:38
- 1 I'm making a data entry form for speedy entry. If the user tabs into the box, I want to scroll so that the element is vertically centered in the screen. But if they click, the effect bees very disorienting, and I don't want it to occur. – Gausie Commented Dec 11, 2009 at 14:43
5 Answers
Reset to default 4May not work 100% but if there isn't a direct way then can't you just use Mouseover
and detect it? 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...).
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/js/keys.html).
What about using mouse position?
In the event handler, pare 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. I think Jquery may provide some functionality for it too.
Try getting the keyCode - here's a link to an article that goes into detail on how. The ments at the bottom may be useful as well.
http://www.geekpedia./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.
<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');
});
Quick question - is it possible to detect whether a focus has e 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
Quick question - is it possible to detect whether a focus has e 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
Share Improve this question edited May 9, 2012 at 15:15 Ruan Mendes 92.3k31 gold badges159 silver badges221 bronze badges asked Dec 11, 2009 at 14:35 GausieGausie 4,3591 gold badge27 silver badges37 bronze badges 2- +1 because it's an interesting question. but i fail to see why you need that distinction. – just somebody Commented Dec 11, 2009 at 14:38
- 1 I'm making a data entry form for speedy entry. If the user tabs into the box, I want to scroll so that the element is vertically centered in the screen. But if they click, the effect bees very disorienting, and I don't want it to occur. – Gausie Commented Dec 11, 2009 at 14:43
5 Answers
Reset to default 4May not work 100% but if there isn't a direct way then can't you just use Mouseover
and detect it? 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...).
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/js/keys.html).
What about using mouse position?
In the event handler, pare 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. I think Jquery may provide some functionality for it too.
Try getting the keyCode - here's a link to an article that goes into detail on how. The ments at the bottom may be useful as well.
http://www.geekpedia./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.
<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');
});
本文标签: Can I detect the source of a focus (JavascriptjQuery)Stack Overflow
版权声明:本文标题:Can I detect the source of a focus? (Javascript, jQuery) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1740890501a1792030.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论