問題描述
知道 jQuery Caret 插件,我仍然看不到以下方法帶有 JavaScript 的 HTML 中的單行文本框(即 input:type=text
控件):
Knowing the jQuery Caret plugin, I'm still seeing no way to do the following on a single line text box in HTML (i.e. the input:type=text
control) with JavaScript:
- 以編程方式將文本插入文本框
- 將插入符號設置到末尾.
- 使插入符號可見(即滾動文本框內容)
- Programmatically inserting text into a text box
- Setting the caret to the end.
- Making the caret visible (i.e. scroll the text box content)
我知道怎么做 1. 和 2.,只是為了 3. 我沒有找到辦法.
I know how to do 1. and 2., just for 3. I have found no way of doing it.
舉例說明:
我的問題是:
有什么方法可以可靠地將插入符號放在文本的末尾并將其滾動到視圖中?
Is there any way to reliable put the caret at the end of the text and scroll it into the view?
(我可以考慮模擬按下 END
鍵或類似的東西,但我不確定這是否是最佳選擇).
(I can think of simulating pressing the END
key or something like that, but I'm unsure whether this would be the best option).
推薦答案
//hack to get cursor at end:
//hack to get cursor at end:
$(textboxselector).val($(textboxselector).val());
應該首先注意焦點:
$(textboxselector).val(newtext);
$(textboxselector).focus();
$(textboxselector).val($(textboxselector).val());
這篇關于可以使用 JavaScript 將插入符號滾動到單個 HTML 文本字段內的視圖中嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!