[英]Fetching cursor location and highlighted text in textarea using JQuery
Having a textarea in a form I am trying to do several things: 在表单中有一个textarea我试图做几件事:
As I am already using JQuery, I'd prefer a solution that works smoothly with that. 由于我已经在使用JQuery,我更喜欢一种能够顺利运行的解决方案。 Any pointers how to achieve the above would be appreciated.
任何指针如何实现上述将是值得赞赏的。
There are many jQuery plugins for this. 有很多jQuery插件。 Here's a good one I've used before:
这是我以前用过的好文章:
http://plugins.jquery.com/project/a-tools http://plugins.jquery.com/project/a-tools
To fetch the current location of the cursor within the text area: 要在文本区域内获取光标的当前位置:
$("textarea").getSelection().start;
To fetch the current selection within the textarea: 要获取textarea中的当前选择:
$("textarea").getSelection();
this returns an object like this: 这会返回一个这样的对象:
{
start: 1, // where the selection starts
end: 4, // where the selection ends
length: 3, // the length of the selection
text: 'The selected text'
}
To insert some text at the current cursor location: 要在当前光标位置插入一些文本:
$("#textarea").insertAtCaretPos("The text to insert");
To replace the current selection by some other text: 要通过其他文本替换当前选择:
$("#textarea").replaceSelection('This text will replace the selection');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.