【发布时间】:2016-01-04 21:31:48
【问题描述】:
这个问题归结为需要在指令中获取<textarea> 元素的光标位置。简而言之,我只需要在这方面工作。
var position = element.selectionStart;
使用各种方法,例如,至少有 5 个关于该主题的答案。 this one for angular, this one for some jQuery extension, enter link description here.
我已阅读所有内容并决定尝试获取选择开始对象。这是我的PLNKR。我不知道我做错了什么。注意 - 背景变化的东西是看我是否选择了正确的元素,我就是。编辑这里是失败的代码的sn-p:
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
附:单击文本区域可立即了解指令中发生的情况。
【问题讨论】:
标签: javascript jquery html angularjs selection