jQuery文本框光标到文本结尾?

前端之家收集整理的这篇文章主要介绍了jQuery文本框光标到文本结尾?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用jQuery基本上替换用户点击“输入”后文本框中文本末尾的光标

我有“输入”部分工作 – 但我不知道如何[在输入部分之后] – 我可以让光标返回到文本框内输入文本的末尾?

即,此时,当用户点击进入时 – 光标转到一个新行,我希望它基本上到达当前文本的末尾?

一些代码

  1. jQuery('#textBox').keyup(function (e) {
  2. if (e.keyCode == 13) {
  3. ... submits textBox
  4. }
  5. jQuery(this).focus(function() {
  6. var val = this.input.value; //store the value of the element
  7. this.input.value = ''; //clear the value of the element
  8. this.input.value = val; //set that value back.
  9. )};
  10. });

解决方法

如果您只是想阻止’enter’键创建换行符,可以使用preventDefault来阻止它.
  1. $("textarea").keypress(function (event) {
  2. if (event.which == '13') {
  3. event.preventDefault();
  4. }
  5. });

fiddle

如果你真的想在输入中的任何地方按下输入以转到输入的末尾,你也可以重置将始终将光标放在输入末尾的值:

  1. $("textarea").keypress(function (event) {
  2. if (event.which == '13') {
  3. var val = $(this).val();
  4. $(this).val('');
  5. $(this).val(val);
  6. event.preventDefault();
  7. }
  8. });

猜你在找的jQuery相关文章