Javascript focus and select not working in FF

前端 未结 4 2048
傲寒
傲寒 2021-01-20 22:02

Using jQuery, the following is not working in FF, but it is in IE

$(this).focus().select();

I looked around for this and found that you cou

4条回答
  •  猫巷女王i
    2021-01-20 22:24

    A solution to this that I just found is to use the below code.

    [elementHere].setSelectionRange(0, [elementHere].value.length);
    

    According to the Mozilla Developer Network documentation, this selects the text but does not focus it. At least for me, this prevented issues with selecting text inside a focus event handler, since selecting the text does not cause the element containing it to be focused again.

提交回复
热议问题