找到< textarea>的当前行的值使用javascript [英] Find value of current line of a <textarea> using javascript
本文介绍了找到< textarea>的当前行的值使用javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何找到textarea的当前行的值?
我知道我必须找到插入位置,但随后找到它之前的所有内容最后的\ n及其后的所有内容到下一个\ n。
我该怎么做?
var caretPos = 53, //但是你得到它
start,结束
;
for(start = caretPos; start> = 0&& myString [start]!=\ n; --start);
for(end = caretPos; end< myString.length&& myString [end]!=\ n; ++ end);
var line = myString.substring(start + 1,end - 1);
How can I find the value of the current line of a textarea?
I know I have to find the caret position, but then find everything before it up to the last \n and everything after it to the next \n.
How can I do this?
解决方案
A simple way would be to just loop:
var caretPos = 53, // however you get it
start, end
;
for (start = caretPos; start >= 0 && myString[start] != "\n"; --start);
for (end = caretPos; end < myString.length && myString[end] != "\n"; ++end);
var line = myString.substring(start + 1, end - 1);
这篇关于找到< textarea>的当前行的值使用javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文