.focus()“将焦点设置在目标输入”将无法在Firefox中工作 [英] .focus() "set focus on a target input" will not work in Firefox

查看:128
本文介绍了.focus()“将焦点设置在目标输入”将无法在Firefox中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入字段。在某些情况下,我希望当用户点击Tab键时将注意力集中在输入字段上。基本上我模仿谷歌的自动填充搜索框的功能。在IE浏览器,Chrome浏览器和Safari浏览器中,下面的代码可以像预期的那样工作,如果从类myInput的文本框中跳出,焦点将停留在文本框中。但在Firefox中,当您从文本框中跳出时,您将转到下一个文本框。 (这是我正在做的一个非常简化的版本,如果我能得到这个工作,我将能够得到我真正的代码工作。)

<$ p
$ b $($。$ my $ b $($。$ my $ b $)$函数()
{
$(this).focus();
$(this).select();
}
);
}
);

PS。请不要使用setTimeout()建议修复。谢谢。

我已经阅读过描述jQuery如何具有.focus()和javascript的.focus()以及它们如何不相同的文章。我明白这一点(我认为),但我仍然无法弄清楚我做错了什么。



ADDED ...
我可以得到它(

$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b $(。myInput)。blur

function()
{
setTimeout($('。myInput')。focus();,1) ;
}
);
}
);

看起来很骇人,但我无法弄清楚为什么Firefox不会将焦点放在模糊上。所以,如果你有任何的答案,这将是很好的知道。

解决方案

正如你使用的jQuery应该使用.focusout()方法。



http://api.jquery.com/focusout /



这与blur事件不同,它支持从父元素检测焦点丢失(换句话说,它支持事件冒泡)。

I have an input field. Under certain conditions I want to keep the user focused on the input field when they hit the Tab key. Basically I'm mimicking the functionality of Google's auto populating search box. I have everything working fine in every browser except the one I never have problems with... Firefox!?

The following code works as expected in IE, Chrome and Safari, if you Tab out of the textbox with class myInput, your focus stays in the textbox. But in Firefox, when you Tab out of the textbox, you will go to the next textbox. (This is a very simplified version of what I am doing. If I can get this to work, I'll be able to get my real code to work.)

$(document).ready(
    function()
    {               
        $(".myInput").blur
        (
            function()
            {
                $(this).focus();
                $(this).select();
            }
        );      
    }
);

PS. Please don't suggest a fix using setTimeout(). Thanks.

I have read articles describing how jQuery has .focus() and javascript has .focus() and how they are not the same. I understand this (I think), but I still am not able to figure out what I am doing wrong.

ADDED... I can get it to work this way

$(document).ready(
    function()
    {               
        $(".myInput").blur
        (
            function()
            {
                setTimeout("$('.myInput').focus();",1);
            }
        );      
    }
);

Seems hackish but I can't figure out why Firefox won't set focus on blur. So, if any of you have the answer, it would be nice to know.

解决方案

As you are using jQuery should use the .focusout() method.

http://api.jquery.com/focusout/

This is distinct from the blur event in that it supports detecting the loss of focus from parent elements (in other words, it supports event bubbling).

这篇关于.focus()“将焦点设置在目标输入”将无法在Firefox中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆