自定义jquery插件的浏览器控制台错误

时间:2012-01-21 00:39:03

标签: jquery browser

我从jQuery Set Cursor Position in Text Area找到了这个简单的jquery插件,它运行得很好。但有一个问题是我在firefox和chrome控制台上遇到错误

Uncaught TypeError: Object #<HTMLInputElement> has no method 'setCursorPosition'

我正在做的事情非常简单如下。

<script type="text/javascript">
//to set text cursor
(function($) {
  $.fn.setCursorPosition = function(pos) {
    if ($(this).get(0).setSelectionRange) {
      $(this).get(0).setSelectionRange(pos, pos);
    } else if ($(this).get(0).createTextRange) {
      var range = $(this).get(0).createTextRange();
      range.collapse(true);
      range.moveEnd('character', pos);
      range.moveStart('character', pos);
      range.select();
    }
  }
})(jQuery);
....
textboxArray.get(0).setCursorPosition(0);
</script>

任何有关我收到上述错误的原因的提示都将不胜感激。

更新:感谢Blender,尴尬地解决了这个问题。

textboxArray.setCursorPosition(0);

1 个答案:

答案 0 :(得分:2)

看起来你的textboxArray元素不是jQuery对象,试试$(textboxArray.get(0)).setCurrsorPosition(0);

相关问题