在AngularJs中获取Textarea光标位置 [英] Get Textarea Cursor Position in AngularJs
问题描述
这个问题归结为需要在指令中获取< textarea>
元素的光标位置。总之,我需要在这方面工作。
This question boils down to needing to get the cursor position of a <textarea>
element in a directive. In short, all I need to work in this.
var position = element.selectionStart;
使用各种方法至少有5个问题的答案。 这一个角度,这是一个jQuery扩展,在此输入链接描述。
There are at least 5 answers on the subject using various methods, e.g. this one for angular, this one for some jQuery extension, enter link description here.
我已经读完所有内容并决定尝试获取选择开始对象。这是我的 PLNKR 。我不知道我做错了什么。注意 - 背景变化的内容是看我是否正在选择正确的元素,就是我。编辑这里是失败的代码片段:
I have read them all and decided to just try to get the selection start object. Here is my PLNKR. I have no idea what I am doing wrong. Note - the background change stuff is to see whether or not I am selecting the right element, which I am. Edit here is a snippet of the code that fails:
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/zh-CN/docs/Mozilla/Tech/XUL/Property/selectionStart
PS点击文本区域立即知道指令中发生了什么。
P.S. Click on text area to immediately know what's happening in directive.
推荐答案
您正在寻找的语法是:
The syntax you are looking for is:
scope.testValue = textArea.prop("selectionStart");
这篇关于在AngularJs中获取Textarea光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!