如何在焦点上垂直设置文本区域/输入中间的默认光标插入符号
How to set the default cursor caret in the middle of textarea/input vertically when on focus
我想知道如何使用 javascript/css 在两行占位符文本之间设置默认光标位置,以便用户输入可以在输入字段中垂直居中(我认为这看起来更好)。
下面的演示
<textarea id="topic_title_input" placeholder="Hi there, I want the cursor caret starts in the middle of these two lines (veritcally) when onfocus"></textarea>
<style>
#topic_title_input{
font-size: 20;
border-color: orange;
color:black;
background-color: white;
/*text-indent:10px;*/
border:1px solid orange;
width:521px;
height:60px;
outline:none;
border-radius:3px;
resize:none;
padding:7px;
}
</style>
不太确定我明白你在问什么,但无论如何都很有趣。这假定您希望实际使用占位符文本并在中间插入插入符号。其次是试图辨别你所说的"垂直"是什么意思。
var input = document.getElementById('topic_title_input');
var inputSpace = document.getElementById('topic_title_alt');
// Proof of concept. Will blow out existing text on each focus!
input.addEventListener('focus', updateTextArea);
inputSpace.addEventListener('focus', withBreak);
function updateTextArea(e) {
var value, pos;
value = input.getAttribute('placeholder');
// Put it anywhere you want
pos = value.indexOf('middle');
input.value = value;
// Normal browsers and IE9+:
input.setSelectionRange(pos, pos);
}
function withBreak(e) {
var value, pos, splitVal, newVal;
value = inputSpace.getAttribute('placeholder');
// Put it anywhere you want
pos = value.indexOf('middle');
newVal = value.substring(0, pos) + "'r'n'r'n";
newVal += value.substring(pos)
inputSpace.value = newVal;
// Normal browsers and IE9+:
inputSpace.setSelectionRange(pos + 1, pos + 1);
}
textarea {
font-size: 20;
border-color: orange;
color: black;
background-color: white;
/*text-indent:10px;*/
border: 1px solid orange;
width: 521px;
height: 60px;
outline: none;
border-radius: 3px;
resize: none;
padding: 7px;
}
<h2>Insert at "middle"</h2>
<textarea id="topic_title_input" placeholder="Hi there, I want the cursor caret starts in the middle of these two lines (veritcally) when onfocus"></textarea>
<h2>This kind of space?</h2>
<textarea id="topic_title_alt" placeholder="Hi there, I want the cursor caret starts in the middle of these two lines with a space (vertically) when onfocus"></textarea>
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 可以't让我的if语句处理js中的html表单输入
- 名称输入的索引
- 如何编写HTML输入的JS内联
- 要求输入在数据列表中
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 让文本输入幻灯片显示输入时的新文本输入?然后向后滑动
- 如何将输入(type=text)从html表单传递到javascript函数
- 单击jquery清除输入值
- 而循环只设置php中输入字段中的第一个值
- 在输入字段中将最小金额设置为
- jQuery自定义验证比较多个输入的序列
- 如何使用JS在中间输入文本中动态插入字符
- 在输入文本框的文本中间插入键盘字符值
- 如何在焦点上垂直设置文本区域/输入中间的默认光标插入符号
- 在 onkeyup 的另一个输入框中显示两个输入值,中间带有“&”
- 将输入定位在导航栏的中间
- 在javascript中是否有任何方法可以暂停一个函数在中间等待输入
- 如何在输入字段名称中间增加输入字段名称
- Bookmarklet -在URL的中间添加一个输入