简体   繁体   English

jQuery递归关注文本框不起作用

[英]Jquery recursive focus on textbox not working

I'm having trouble with jquery focus. 我在使用jquery焦点时遇到麻烦。

So here's what I want to accomplish: 所以这是我要完成的事情:

To solve ie placeholder issue, I want to focus to an element and then blur it to make the placeholder appear. 为了解决占位符问题,我想关注一个元素,然后对其进行模糊处理以使占位符出现。 I'm currently using it on a modal form. 我目前正在以模式形式使用它。

It works fine on the first load of the modal form, but closing the form and opening it again seems not to work. 在模态表单的第一个加载上它可以正常工作,但是关闭表单然后再次打开它似乎不起作用。

So far I've narrowed down the issue to focus not being triggered by $("#element").focus() more than once. 到目前为止,我已经缩小了范围,以使焦点不再被$(“#element”)。focus()触发。

here is a fiddle to illustrate the issue 这是用来说明问题的小提琴

http://jsfiddle.net/Ere2N/1/ http://jsfiddle.net/Ere2N/1/

JS: JS:

var i = 5;

while(i>0){
    focusMe();
    i--;
}

function focusMe(){
    setTimeout(function(){
        $('#text1').focus().queue(function(){
        setTimeout(function() {
            $('#text2').focus().queue(function(){
                setTimeout(function(){$('#text1').focus();},100);
                setTimeout(function() {
                    $('#text2').focus();
                }, 300);
            })
        }, 300);
        });                
    },100);
}

$("#text1").focus(function(){
    $("#count1").html(($("#count1").html()*1) + 1);
});

$("#text2").focus(function(){
    $("#count2").html(($("#count2").html()*1) + 1);
});

HTML: HTML:

<input type="text" id="text1" />
<input type="text" id="text2" />
<input type="text" id="text3" />

<div id="count1">0</div>
<div id="count2">0</div>

it behaving funny.. 它表现得很好..

1st execution of the function is good. 第一次执行该功能是好的。

However 2nd to last execution only executes the first #text1 focus, thus producing the resulting statistics of 6:2. 但是,从倒数第二次执行仅执行第一个#text1焦点,因此产生的统计信息为6:2。

Any ideas guys? 有想法吗?

.focus仅运行您在焦点事件上编写的代码,使用“ trigger”或使用getElementById()。focus

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

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