[英]EXTJS 5: Get the current cursor position in a textfield or lookupfield
我需要找到光标的当前位置,以便将其存储在变量中。 我看了一些SO答案,但都没有成功。
到目前为止,我有:
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;
};
例如:我有一个显示数据的文本字段。
我的话
当我将光标放在Y和W之间时,它应该返回2。我已经看到了一些旧版本IE的示例,但似乎没有一个适用于Chrome。
有谁知道该怎么做?
var textarea=document.getElementById('textarea'); textarea.onclick=function(){ setTimeout(function(){ console.log(textarea.selectionStart); },1); }
<input id="textarea" type="text" value="MYWORD">
我已经在文本字段的extjs(2+)的旧版本中尝试了此代码,并且对我有用(刚刚在IE上测试)
这是关于从DOM捕获位置。
var position = item.el.dom.selectionStart;
在您的示例中,它将像:
function getCaretPos(id) {
var element = Ext.getCmp(id);
var currSelection = element.el.dom.selectionStart;
}
如果要更改位置,只需将de selectionEnd值设置为所需位置。
element.el.dom.selectionEnd = position;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.