简体   繁体   English

focus() 在 safari 或 chrome 中不起作用

[英]focus() not working in safari or chrome

I have a div that has been given a tabindex, when the div is focused(click or tabbed to) it does the following:我有一个已被赋予 tabindex 的 div,当 div 聚焦(单击或选项卡)时,它会执行以下操作:

inserts an input into itself, gives the input focus将输入插入到自身中,给出输入焦点

this works great in FF, IE and Opera这在 FF、IE 和 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).但是在 Chome/Safari 中,它提供了输入焦点,但实际上并没有将光标放在输入中(我知道它给了它焦点,因为出现了 safari/chrome 焦点边框)。

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.关于此的另一个奇怪之处在于,跳转到 div 将触发 div.focus() 函数并正确地提供输入焦点......只是点击失败。 I tried putting a .click() function on the div to do the same as the focus, but it's not working.我尝试在 div 上放置一个 .click() 函数来执行与焦点相同的操作,但它不起作用。

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.只需在焦点上添加一个 0 计时器......出于某种原因,它只是给了它足够的时间将输入完全加载到 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 :-)如果其他人可以进一步解释这一点或有更好的答案,请随时上台:-)

Although I couldn't find this specifically stated anywhere, .focus() only works on input elements and links.尽管我在任何地方都找不到具体说明,但.focus()仅适用于输入元素和链接。 It also isn't supported properly in Chrome and Safari. Chrome 和 Safari 也没有正确支持它。 I posted a demo here to show you what I mean.我在这里发布了一个演示来向你展示我的意思。 Also note that focus() and focusin() (v1.4) have the same results.另请注意, focus()focusin() (v1.4) 具有相同的结果。

So that being determined, try changing your function to .click()确定后,尝试将您的功能更改为.click()

$("#recipientsDiv").click(function(e){ ... })

set the tabIndex of 'toInput' to 0 or higher, its a known Chrome bug:将 'toInput' 的 tabIndex 设置为 0 或更高,这是一个已知的 Chrome 错误:

http://code.google.com/p/chromium/issues/detail?id=467043 http://code.google.com/p/chromium/issues/detail?id=467043

Your problem is likely that you're not appending a DOM object, you're appending explicit HTML to your page -- and I doubt that Safari is updating the DOM behind the scenes.您的问题很可能是您没有附加 DOM 对象,而是将显式 HTML 附加到您的页面——我怀疑 Safari 是否在幕后更新 DOM。

Try to use the actual DOM methods like document.createElement() to append your input to the DOM, as described in a number of places (such as here or here or here ), and then see if the Safari problem persists.尝试使用诸如document.createElement()类的实际 DOM 方法将您的input附加到 DOM,如许多地方所述(例如此处此处此处),然后查看 Safari 问题是否仍然存在。

All that said, the way that you describe the issue arising -- on clicks but not tabs, for example -- would argue that the problem isn't going to be this... so now I'm curious.综上所述,你描述出现的问题的方式——例如,点击而不是标签——会争辩说问题不会是这个......所以现在我很好奇。 (In any event, using DOM methods is really the right way to add elements, so it's not a bad idea to do it anyway.) (无论如何,使用 DOM 方法确实是添加元素的正确方法,因此无论如何这样做也不错。)

根据html 4.01 标准,tabindex 不适用于 div。

I got a similar problem with the latest chrome release, and I found out that I had in my css-reset the following我在最新的 chrome 版本中遇到了类似的问题,我发现我的 css-reset 中有以下内容

-webkit-user-select: none; 
-khtml-user-select: none; 
-moz-user-select: none; 
-o-user-select: none; 
user-select: none;

the result was that in chrome i couldn't even input text... in firefox it was possible结果是在 chrome 中我什至无法输入文本......在 firefox 中这是可能的

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

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