问题描述
我有几个使用默认值的输入字段。我使用Tab键定义了tabindexes从一个字段跳转到另一个字段。问题是,当我到达一个字段时,整个文本被选中,并且如果我开始键入,它将被替换。我想将光标位置设置为预填充文本的末尾。我怎样才能做到这一点?
I have several input fields with default values. I defined tabindexes to jump from field to field with the tab-button. The problem is that when I get to a field the whole text gets selected, and it is replaced if I start to type. I would like to set the cursor position to the end of the pre-filled text. How can I do this ?
推荐答案
通过在每个节点上定义onfocus事件。并设置插入位置到最后。由于默认的焦点/点击行为,设置游标应该被延迟。
By defining onfocus event on every node. And setting caret position to the end. Because of the default focus/click behavior, setting cursor should be deferred.
function setCursorAtEnd(node){ setTimeout(function(){ setCursor(node,node.value.length); },10); } <input onfocus="setCursorAtEnd(this)" value="bla"/>
您可以在此找到 setCursor 函数问题:
The setCursor function you can find in this question: Set cursor at a length of 14 onfocus of a textbox
这篇关于在表单输入字段中定义光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!