Javascript 焦点在 IE 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2600186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:04:52  来源:igfitidea点击:

focus doesn't work in IE

javascriptjqueryhtmlcss

提问by Simon

i have the following function

我有以下功能

 function change() 
 {
       var input = document.getElementById('pas');
       var input2 = input.cloneNode(false);
       input2.type = 'password';
       input.parentNode.replaceChild(input2,input);
       input2.focus();
  }

but focus()doesn't work in ie7, so what can i do! i want to have the cursor inside of input!

focus()在 ie7 中不起作用,那我该怎么办!我想让光标在输入里面!

thanks

谢谢

update

更新

great solution, thanks, but now it doesn't work in opera:(

很好的解决方案,谢谢,但现在它在歌剧中不起作用:(

回答by zaf

For IE you need to use a settimeout function due to it being lazy, for example:

对于 IE,您需要使用 settimeout 函数,因为它很懒惰,例如:

setTimeout(function() { document.getElementById('myInput').focus(); }, 10);

From http://www.mkyong.com/javascript/focus-is-not-working-in-ie-solution/

来自http://www.mkyong.com/javascript/focus-is-not-working-in-ie-solution/

For opera, this may help: how to set focus in required index on textbox for opera

对于歌剧,这可能会有所帮助: how to set focus in required index on textbox for 歌剧

回答by Bojan Tadic

We hit the same issue. For focusing we are using General function which is applying settimeout solution mentioned in: http://www.mkyong.com/javascript/focus-is-not-working-in-ie-solution/with 100 milliseconds.

我们遇到了同样的问题。为了聚焦,我们正在使用 General 函数,该函数应用了以下提到的 settimeout 解决方案:http://www.mkyong.com/javascript/focus-is-not-working-in-ie-solution/ 100 毫秒。

Still on some screens it's not working properly. Especially when iframes are included. There is another known and similar IE issue:
IE 9 and IE 10 cannot enter text into input text boxes from time to time -> IE 9 and IE 10 cannot enter text into input text boxes from time to time

仍然在某些屏幕上它不能正常工作。特别是当包含 iframe 时。还有一个已知的类似 IE 问题:
IE 9 和 IE 10 有时无法在输入文本框中输入文本 -> IE 9 和 IE 10有时 无法在输入文本框中输入文本

What I have noticed is when you have focus, without pointer, you can apply workaround by pressing TAB key (focus on next element) and than SHIFT+TAB which will return to our target element with focus and typing pointer. In order to be sure we can type inside input we focus on random element and then on our target input.

我注意到的是,当你有焦点时,没有指针,你可以通过按 TAB 键(聚焦下一个元素)而不是 SHIFT+TAB 来应用解决方法,这将返回到我们的目标元素,并带有焦点并输入指针。为了确保我们可以输入内部输入,我们专注于随机元素,然后是我们的目标输入。

$('body').focus();
n.focus();

So we applied the same solution in javascript/JQuery in our general focus function. So there is an if statement

因此,我们在 javascript/JQuery 中的常规焦点功能中应用了相同的解决方案。所以有一个 if 语句

...        
if($.browser.msie) {
    setTimeout(function() { try {
        $('body').focus(); //First focus on random element
        $(n).focus(); //Now focus on target element
    } catch (e) { /*just ignore */ } }, 100); //See http://www.mkyong.com/javascript/focus-is-not-working-in-ie-solution/
} else { //Standard FF, Chrome, Safari solution...
...

To be sure since there is big regression we are still keeping solution with settimeout as a backup. Tested on IE10, IE11, Firefox 45, Chrome 49.0.2623.87

可以肯定的是,由于存在较大的回归,我们仍然保留使用 settimeout 作为备份的解决方案。在 IE10、IE11、Firefox 45、Chrome 49.0.2623.87 上测试

回答by Rajnish Kumar

In Case you are looking to set focus in 1st input element of last row in table.Name of my div where i have kept my table is tableDivand i am setting focus to last row's 1st inputtext

如果您希望在表中最后一行的第一个输入元素中设置焦点。我保存表的 div 的名称是tableDiv,我将焦点设置到最后一行的第一个输入文本

setTimeout(function(){                
    $($('#tableDiv  tr:last').find('input[type=text]')[0]).focus();
},2);

回答by Adam Targosz

@Bojan Tadic THANK YOU!

@Bojan Tadic 谢谢!

Below Code did the trick :)

下面的代码做到了:)

$('body').focus(); //First focus on random element

I think the issue comes up when you use input and a placeholder. Managed so solved this thanks to this answer, I was missing that $(body).focus. Made this code to run only on IE so that all my inputs can be freely accessed by 'tabbing'. Previously when I had only tabIndex on my inputs I was able to move to the next one but focus wasn't complete and couldn't write anything in it.

我认为当您使用输入和占位符时会出现问题。由于这个答案,管理如此解决了这个问题,我错过了 $(body).focus。使此代码仅在 IE 上运行,以便可以通过“tabbing”自由访问我的所有输入。以前,当我的输入中只有 tabIndex 时,我能够移动到下一个,但焦点未完成并且无法在其中写入任何内容。

This is complete code.

这是完整的代码。

$('input[name^="someName"]').on('keydown', function(e){
    var keyCode = e.which || e.keyCode;
    if(keyCode === 9){
        e.preventDefault();
        $('body').focus();
        var nextTabIndex = parseInt($(this).attr("tabIndex"));
        nextTabIndex++;
        setTimeout(function(){$('input[tabIndex=' + nextTabIndex +']')[0].focus();},20);
    }
});

回答by markiyanm

I've had the same issue and was able to get IE to work using code behind by making a SetInitialFocus function and calling it in my PageLoad function.

我遇到了同样的问题,并且通过创建 SetInitialFocus 函数并在我的 PageLoad 函数中调用它,能够让 IE 使用隐藏的代码工作。

Take a look at the following example and give it a shot, it worked for me. http://www.cambiaresearch.com/c4/df9f071c-a9eb-4d82-87fc-1a66bdcc068e/Set-Initial-Focus-on-an-aspnet-Page.aspx

看看下面的例子并试一试,它对我有用。 http://www.cambiaresearch.com/c4/df9f071c-a9eb-4d82-87fc-1a66bdcc068e/Set-Initial-Focus-on-an-aspnet-Page.aspx

回答by ajai

function change() {
    var input = document.getElementById('pas');
    var input2 = input.cloneNode(false);
    input2.type = 'password';
    input.parentNode.replaceChild(input2, input);
    setTimeout(function () {
        input2.focus();
    }, 10);
}

回答by Kangkan

IE7 does not support the focus()method. I don't see any method.

IE7 不支持该focus()方法。我没有看到任何方法。

回答by Chris Love

Its is very easy using jQuery, not sure why you are doing it the hard way :) In this example I have a class assigned to the input field I want the initial focus set called initFocus. You can use any selector you want to find your element. from your code I would use $("#pas").focus();

使用 jQuery 很容易,但不知道为什么你要这样做困难:) 在这个例子中,我有一个类分配给输入字段,我想要一个名为 initFocus 的初始焦点集。您可以使用任何要查找元素的选择器。从你的代码我会使用 $("#pas").focus();

$(".initFocus").focus();