指定在哪里集中在Textarea [英] Specifying Where To Focus Within Textarea

查看:117
本文介绍了指定在哪里集中在Textarea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个textarea ...

 < textarea> 
一些写在这里
*
然后在另一行
< / textarea>

我想要做的是把用户放在*所在的位置。这是可能的吗?

解决方案

使用下面的函数在textarea中设置一个选择。

 函数setSelRange(inputEl,selStart,selEnd){
if(inputEl.setSelectionRange){
inputEl.focus();
inputEl.setSelectionRange(selStart,selEnd);
} else if(inputEl.createTextRange){
var range = inputEl.createTextRange();
range.collapse(true);
range.moveEnd('character',selEnd);
range.moveStart('character',selStart);
range.select();


//从http://www.webmasterworld.com/forum91/4527.htm

因此,您可以搜索 * 字符的位置,并在如下所示的调用中使用该值:

  var pos = 17; //将其设置为*字符的位置。 
setSelRange(document.getElementById('textareaId'),pos,pos);


I have a textarea...

<textarea>
Some writing here
*
Then some more on another line
</textarea>

What I want to do is focus the user where the * is. Is this possible?

解决方案

Use the following function to set a selection within a textarea.

function setSelRange(inputEl, selStart, selEnd) { 
   if (inputEl.setSelectionRange) { 
     inputEl.focus(); 
     inputEl.setSelectionRange(selStart, selEnd); 
   } else if (inputEl.createTextRange) { 
     var range = inputEl.createTextRange(); 
     range.collapse(true); 
     range.moveEnd('character', selEnd); 
     range.moveStart('character', selStart); 
     range.select(); 
   } 
}
// From http://www.webmasterworld.com/forum91/4527.htm

So, in your case, you can search for the position of the * character and use that value in a call like this:

var pos = 17; // Set this to the position of the * character.
setSelRange(document.getElementById('textareaId'), pos, pos);

这篇关于指定在哪里集中在Textarea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆