简体   繁体   English

在文本区域“内部”加粗文本

[英]Boldening text “Inside” a textarea

I have a textarea #myarea into which I'm typing text.我有一个 textarea #myarea ,我正在其中输入文本。 I've typed this text.我已经输入了这个文本。

This is a sentence and only this word will end up being bolded这是一个句子,只有这个词最终会被加粗

Now I have a button on the side, like a bold button.现在我在侧面有一个按钮,就像一个粗体按钮。 I want to select the letters I want bolded, click the button, and see those letters turn bold inside the textarea.我想 select 我想加粗的字母,单击按钮,然后看到这些字母在文本区域内变为粗体。 It's similar to the standard functionality found in many editors, but I'm looking for something much simpler as I described here.它类似于许多编辑器中的标准功能,但我正在寻找更简单的东西,就像我在这里描述的那样。

So is there a way to do this with jquery?那么有没有办法用 jquery 做到这一点? I'm guessing it's a 2-part problem.我猜这是一个两部分的问题。 I need to find the letters selected, then apply bold to them so it shows bold inside the textarea.我需要找到选定的字母,然后对它们应用粗体,使其在文本区域内显示为粗体。

If you don't mind using a div that has its ContentEditable property set to true then you can easily do this using:如果您不介意使用其 ContentEditable 属性设置为 true 的 div,那么您可以使用以下方法轻松完成此操作:

document.execCommand('bold',null,false);

You can easily style this div to look and feel like a textarea.您可以轻松地设置此 div 的样式,使其看起来和感觉像一个 textarea。

See jsFiddle here: http://jsfiddle.net/hqvDT/1/在此处查看 jsFiddle: http://jsfiddle.net/hqvDT/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM