EXTJS 5:获取当前光标在文本字段或查找字段中的位置 [英] EXTJS 5: Get the current cursor position in a textfield or lookupfield
问题描述
我需要找到光标的当前位置,以便将其存储在变量中。我看过一些SO答案,但都没有成功。
I need to find the current position of the cursor so I can store it in a variable. I've looked at a few SO answers and none have been successful.
到目前为止,我已经了解了什么:
What I have so far:
function getCaretPos(id) {
var el = Ext.getCmp(id);
var rng, ii=-1;
var currSelection = window.getSelection();
if(currSelection){
currSelection.modify("character", el.value.length);
// get the current position
// ii = currentPosition
}
return ii;
};
例如:我有一个显示数据的文本字段。
For example: I have a textfield displaying data.
MYWORD
当我将光标置于Y和W之间时,它应该返回2。我已经看到了一些旧版本IE的示例,但似乎没有在Chrome中为我工作。
When I place the cursor between the Y and W it should return 2. I've seen a few examples for older versions of IE but none seem to work for me in Chrome.
有人知道如何做到这一点吗?
Does anyone know how this can be done?
推荐答案
我已经在文本字段的extjs(2+)的旧版本中尝试了此代码,并且对我有用(刚刚在IE上测试过)
I have tried this code in an old version of extjs (2+) on a textfield and it works for me (just tested on IE)
这是关于从DOM捕获位置。
It's about capture the position from the DOM.
var position = item.el.dom.selectionStart;
在您的示例中,它将类似于:
In your example it will be like:
function getCaretPos(id) {
var element = Ext.getCmp(id);
var currSelection = element.el.dom.selectionStart;
}
如果要更改位置,只需将de selectionEnd值设置为
If you want to change the position you just need to set de selectionEnd value to the position you want.
element.el.dom.selectionEnd = position;
这篇关于EXTJS 5:获取当前光标在文本字段或查找字段中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!