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

后端 未结 10 1159
猫巷女王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:12

    This is a solution working in Firefox, Chrome and IE, both with keyboard focus and mouse focus. It also handles correctly clicks following the focus (it moves the caret and doesn't reselect the text):

    
    

    With keyboard focus, only onfocus triggers which selects the text because this.clicked is not set. With mouse focus, onmousedown triggers, then onfocus and then onclick which selects the text in onclick but not in onfocus (Chrome requires this).

    Mouse clicks when the field is already focused don't trigger onfocus which results in not selecting anything.

提交回复
热议问题