javascript 如何从jquery的文本区域中获取选定的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16036745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:59:55  来源:igfitidea点击:

How to get selected text from a text area in jquery?

javascriptjquery

提问by Naveenbos

If i select a text from a text area i need to know which text is selected, for example "hello world" if i select hello i would like to see hello is selected how can i do this in jquery. and i want to make it bold.

如果我从文本区域中选择一个文本,我需要知道选择了哪个文本,例如“hello world”,如果我选择 hello 我希望看到 hello 被选中我如何在 jquery 中执行此操作。我想让它大胆。

<textarea id="editor" cols="80" rows="20">
   Hello world, This is Me!
</textarea> 
<button onclick="SelectText()">
  Bold
</button>

回答by Rodrigo Techera

About to make the selected content as bold, you can't do that directly in the textarea but you can do that using editable divs. This plugin could help you: http://mistic100.github.io/jquery-highlighttextarea/

要将所选内容设为粗体,您不能直接在 textarea 中执行此操作,但可以使用可编辑的 div 执行此操作。这个插件可以帮助你:http: //mistic100.github.io/jquery-highlighttextarea/

About your comment: "How can i get the all text from a div excluding html tags?" you can use something like this: jQuery('.yourDiv').text();

关于您的评论:“如何从 div 中获取除 html 标签之外的所有文本?” 你可以使用这样的东西:jQuery('.yourDiv').text();