focus() not working in safari or chrome

前端 未结 6 1982
别那么骄傲
别那么骄傲 2020-11-30 11:25

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

相关标签:
6条回答
  • 2020-11-30 11:35

    Although I couldn't find this specifically stated anywhere, .focus() only works on input elements and links. It also isn't supported properly in Chrome and Safari. I posted a demo here to show you what I mean. Also note that focus() and focusin() (v1.4) have the same results.

    So that being determined, try changing your function to .click()

    $("#recipientsDiv").click(function(e){ ... })
    
    0 讨论(0)
  • 2020-11-30 11:37

    set the tabIndex of 'toInput' to 0 or higher, its a known Chrome bug:

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

    0 讨论(0)
  • 2020-11-30 11:40

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

    0 讨论(0)
  • 2020-11-30 11:44

    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.

    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.

    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.)

    0 讨论(0)
  • 2020-11-30 11:49

    according to the html 4.01 standard, tabindex does not apply to divs.

    0 讨论(0)
  • 2020-11-30 11:52

    I got a similar problem with the latest chrome release, and I found out that I had in my css-reset the following

    -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

    0 讨论(0)
提交回复
热议问题