这个问题归结为需要获取 <textarea>
的光标位置指令中的元素。简而言之,我需要做的就是这方面的工作。
var position = element.selectionStart;
使用各种方法至少有 5 个关于该主题的答案,例如this one for angular , this one for some jQuery extension , enter link description here .
我已全部阅读并决定尝试获取选择起始对象。这是我的 PLNKR .我不知道我做错了什么。注意 - 背景变化的东西是看我是否选择了正确的元素,我是。此处编辑失败的代码片段:
link: function(scope, elem, attrs) {
var textArea = elem.find("#itemTextArea");
textArea.bind('click', function() {
//This gets to undefined, even though selectionStart is a property of textarea*
scope.testValue = elem.selectionStart;
});
},
* https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Property/selectionStart
附言单击文本区域可立即了解指令中发生的情况。
最佳答案
您正在寻找的语法是:
scope.testValue = textArea.prop("selectionStart");
关于javascript - 在 AngularJs 中获取文本区域光标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34600231/