天天看點

html 文本 選擇,單擊時選擇HTML文本輸入中的所有文本

html 文本 選擇,單擊時選擇HTML文本輸入中的所有文本

隔江千裡

先前釋出的解決方案有兩個怪癖:在Chrome浏覽器中,通過.select()進行的選擇不粘滞-添加輕微的逾時即可解決此問題。聚焦後,無法将光标放置在所需的位置。這是一個完整的解決方案,可以選擇所有焦點上的文本,但是允許在焦點之後選擇特定的光标點。        $(function () {            var focusedElement;            $(document).on('focus', 'input', function () {                if (focusedElement == this) return; //already focused, return so user can now place cursor at specific point in input.                focusedElement = this;                setTimeout(function () { focusedElement.select(); }, 50); //select all text in any field on focus for easy re-entry. Delay sightly to allow focus to "stick" before selecting.            });        });