焦点()不工作在Safari或铬 [英] focus() not working in safari or chrome

查看:126
本文介绍了焦点()不工作在Safari或铬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div给了一个tabindex,当div被集中(点击或选中)时,它执行以下操作:

将输入插入本身,
给出了输入焦点



这个在FF,IE和Opera中很好用,但是在

Chome / Safari它提供了输入焦点,但实际上并没有把光标放在输入内(我知道它给焦点,因为safari / chrome焦点边框出现了)。

任何建议到底是怎么回事?

我必须修复密钥处理程序,所以方向键和退格键也可以工作,如果你愿意的话。

预先感谢您!

以下是代码示例:

p>

  var recipientDomElem = $(#recipientsDiv); 
recipientDomElem [0] .tabIndex = 0;
$(#recipientsDiv)。focus(function(e){
var code =(e.keyCode?e.keyCode:e.which);
window.clearTimeout(statusTimer) ;
recipientDivHandler(code,null);
});

$ b函数recipientDivHandler(code,element){
$(#recipientsDiv)。append('< input type =textid =toInputclass = inlineBlockElement rightSpacerstyle =border:0px none #ffffff; padding:0px; width:40px; margin-bottom:3px; padding:0; overflow:hidden; font-size:11px;/>');
$(#toInput)。focus();
}

另外一个奇怪的情况是,切换到div会触发div。 focus()函数并正确给出输入焦点...这只是失败的点击。我试图把一个.click()函数在div上做同样的焦点,但它不工作。

解决方案

我自己就得到了答案......它可能看起来很虚弱,太简单了......但是它很有效。



准备好了这个真棒..?



只需在焦点上添加一个0的计时器...出于某种原因,它只是给了足够的时间来完全加载输入到DOM中。

 函数recipientDivHandler(code,element){
$(#recipientsDiv)。append('< input type =textid = toInputclass =inlineBlockElement rightSpacerstyle =border:0px none #ffffff; padding:0px; width:40px; margin-bottom:3px; padding:0; overflow:hidden; font-size:11px;/> );
setTimeout(function(){$(#toInput)。focus();},0);





$ b如果别人可以进一步解释这个或者有更好的答案,请随意采取阶段: - )


I have a div that has been given a tabindex, when the div is focused(click or tabbed to) it does the following:

inserts an input into itself, gives the input focus

this works great in FF, IE and Opera

but in Chome/Safari it gives the input focus but fails to actually put the cursor inside the input (I know it gives it focus because the safari/chrome focus borders appear).

Any suggestions as to what is going on?

I have to fix the key handler after this so the arrow keys and backspace keys work too, feel free to chime in on that if you'd like.

Thank you in advance!

Here's a sample of the code:

var recipientDomElem = $("#recipientsDiv");
recipientDomElem[0].tabIndex = 0;
$("#recipientsDiv").focus(function(e){
var code = (e.keyCode ? e.keyCode : e.which);
window.clearTimeout(statusTimer);
recipientDivHandler(code, null);
});


function recipientDivHandler(code, element){
$("#recipientsDiv").append('<input type="text" id="toInput" class="inlineBlockElement rightSpacer" style="border:0px none #ffffff; padding:0px; width:40px;margin-bottom:3px;padding:0; overflow:hidden; font-size:11px;" />');
$("#toInput").focus();
}

Another oddity about this is that tabbing through to the div will fire the div.focus() function and correctly give the input focus...it's just the click that fails. I tried putting a .click() function on the div to do the same as the focus, but it's not working.

解决方案

I got the answer on my own...it might seem weak, and too simple...but it works.

Ready for this awesomeness..?

Just add a timer of 0 to the focus...for some reason it just gives it enough time to fully load the input into the DOM.

function recipientDivHandler(code, element){
$("#recipientsDiv").append('<input type="text" id="toInput" class="inlineBlockElement rightSpacer" style="border:0px none #ffffff; padding:0px; width:40px;margin-bottom:3px;padding:0; overflow:hidden; font-size:11px;" />');
setTimeout(function(){$("#toInput").focus();},0);
}

If someone else can further explain this or has a better answer please feel free to take the stage :-)

这篇关于焦点()不工作在Safari或铬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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