【问题标题】:Get Textarea Cursor Position in AngularJs在AngularJs中获取Textarea光标位置
【发布时间】: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


    【解决方案1】:

    您要查找的语法是:

    scope.testValue = textArea.prop("selectionStart");
    

    【讨论】:

    • 哇。我花了一天的时间试图解决这个问题。我想这就是我不使用 jQuery 并识别它的结果。非常非常感谢你。马上接受。
    • 很高兴能帮到你。
    【解决方案2】:

    这似乎也有效

    scope.testValue = textArea[0].selectionStart;

    【讨论】:

    • 感谢您的回复。接受 jbrown 的回答,因为他是第一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多