Y'know how textmate calculates which row and column you are on? I've been struggling to do this with jQuery and a textarea.
To those who 开发者_JAVA技巧haven't use textmate, is there any possible way I can get the row number of the current textarea position?
You can use the Caret Range plugin for this, like this:
var range = $('textarea').caret().start;
//divide by columns to get row, modulus by columns to get column position
This only works on a fixed-width font though, if it wasn't fixed-width, you'll have to search for line returns and calculate it out that way, but the selection/range is the tricky part...you can use the plugin or see how it gets the selection here.
Side note: It's very non-cross-browser, all plugins/editors like this (currently) have browser-specific case handlers, as older browsers drop out of sight this can be slimmed down a bit.
精彩评论