3
我有一連串的文本框,用於輸入時間。當用戶回到已編輯的文本框時,瀏覽器會突出顯示該文本框中的文本。如何在文本框獲取焦點時取消選擇文本,當選項卡被按下時?
- 我怎麼會取消選擇它的文本時,文本框獲得焦點,並
- 將光標移動到文本框中的文本的開始?
我曾嘗試以下方法,但在沒有更好的:
在textboxfocushandler:
var html = $("#MyTextArea").val();
$("#MyTextArea").focus().val("").val(html);
的文本框選擇處理程序:
if (window.getSelection) {
if (window.getSelection().empty) { // Chrome
window.getSelection().empty();
} else if (window.getSelection().removeAllRanges) { // Firefox
window.getSelection().removeAllRanges();
}
} else if (document.selection && document.selection.empty) { // IE?
document.selection.empty();
}
[Check this,it's similar](http://stackoverflow.com/questions/11137794/javascript-focus-remove-text-highlight/11137907#11137907)。 – 2013-04-07 00:08:36