problem with Chrome form handling: input onfocus=“this.select()”

后端 未结 10 1192
猫巷女王i
猫巷女王i 2020-12-14 07:56

I\'m using the following HTML code to autoselect some text in a form field when a user clicks on the field:



        
10条回答
  •  鱼传尺愫
    2020-12-14 08:19

    The way I got around this was by creating a wrapper function that uses setTimeout() to delay the actual call to select(). Then I just call that function in the focus event of the textbox. Using setTimeout defers the execution until the call stack is empty again, which would be when the browser has finished processing all the events that happened when you clicked (mousedown, mouseup, click, focus, etc). It's a bit of a hack, but it works.

    function selectTextboxContent(textbox)
    {
        setTimeout(function() { textbox.select(); }, 10);
    }
    

    Then you can do something like this to do the selection on focus:

    
    

提交回复
热议问题