可以使用 JavaScript 将插入符号滚动到单个 HTML 文本字段内的视图中吗?

Possible to scroll caret into view inside a single HTML text field with JavaScript?(可以使用 JavaScript 将插入符号滚动到单个 HTML 文本字段内的视图中吗?)
本文介绍了可以使用 JavaScript 将插入符号滚动到单个 HTML 文本字段内的视图中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

知道 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:

  1. 以编程方式将文本插入文本框
  2. 将插入符号设置到末尾.
  3. 使插入符号可见(即滚动文本框内容)
  1. Programmatically inserting text into a text box
  2. Setting the caret to the end.
  3. 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 文本字段内的视图中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)