焦点在IE中不起作用

时间:2020-12-02 21:44:39

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:(

很好的解决方案,谢谢,但是现在在opera上行不通了。

7 个解决方案

#1


28  

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

对于opera来说,这可能会有所帮助:如何在opera的文本框中设置必需的索引

#2


1  

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.

我们遇到了同样的问题。为了集中注意力,我们使用的是通用函数,它使用了http://www.mkyong.com/javascript/focus-is-not-working-in- e-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问题:ie9和ie10不能在输入文本框中不时地输入文本——> ie9和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来解决问题,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上测试

#3


0  

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

IE7不支持focus()方法。我看不出有什么办法。

#4


0  

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 - 4 d82 - 87 - fc - 1 - a66bdcc068e/set -初始焦点- - - aspnet page.aspx

#5


0  

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);
}

#6


0  

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 tableDiv and i am setting focus to last row's 1st inputtext

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

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

#7


-2  

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();

#1


28  

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

对于opera来说,这可能会有所帮助:如何在opera的文本框中设置必需的索引

#2


1  

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.

我们遇到了同样的问题。为了集中注意力,我们使用的是通用函数,它使用了http://www.mkyong.com/javascript/focus-is-not-working-in- e-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问题:ie9和ie10不能在输入文本框中不时地输入文本——> ie9和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来解决问题,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上测试

#3


0  

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

IE7不支持focus()方法。我看不出有什么办法。

#4


0  

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 - 4 d82 - 87 - fc - 1 - a66bdcc068e/set -初始焦点- - - aspnet page.aspx

#5


0  

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);
}

#6


0  

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 tableDiv and i am setting focus to last row's 1st inputtext

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

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

#7


-2  

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();