开发者

jQuery - inserting a string inside a textarea, then selecting a portion of it

开发者 https://www.devze.com 2023-02-23 08:06 出处:网络
Like SO has for example the B button: **strong text** But strong t开发者_Go百科ext to be automatically selected, and the cursor to be positioned just before the sYou could use my Rangy Inputs jQuery

Like SO has for example the B button:

**strong text**

But strong t开发者_Go百科ext to be automatically selected, and the cursor to be positioned just before the s


You could use my Rangy Inputs jQuery plug-in now that I've finally got round to documenting it. Using it, the following will do the job:

$textArea = $("#yourtextarea");
$textArea.focus();
$textArea.surroundSelectedText("**", "**");

This will select the same text as was selected before, which is what SO does. However, if you prefer to have a caret before the "s" as you say then you can add the following:

$textArea.collapseSelection(true);

jsFiddle example: http://jsfiddle.net/AL7uY/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号