Cursor position in a textarea (character index, not x/y coordinates)
问题 How can I get the caret\'s position in a textarea using jQuery? I\'m looking for the cursor\'s offset from the start of the text, not for the (x, y) position. 回答1: Not jQuery, but just Javascript... var position = window.getSelection().getRangeAt(0).startOffset; 回答2: Modified BojanG's solution to work with jQuery. Tested in Chrome, FF, and IE. (function ($, undefined) { $.fn.getCursorPosition = function() { var el = $(this).get(0); var pos = 0; if('selectionStart' in el) { pos = el