粗体显示“内部"一个文本区域 [英] Boldening text "Inside" a textarea

查看:91
本文介绍了粗体显示“内部"一个文本区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要在其中键入文本的文本区域#myarea.我已经输入了这段文字.

I have a textarea #myarea into which I'm typing text. 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. It's similar to the standard functionality found in many editors, but I'm looking for something much simpler as I described here.

那么有没有办法用jquery做到这一点?我猜这是一个两部分的问题.我需要找到选定的字母,然后对它们应用粗体,以便在文本区域内将其显示为粗体.

So is there a way to do this with 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.

推荐答案

如果您不介意使用其ContentEditable属性设置为true的div,则可以使用以下方法轻松完成此操作:

If you don't mind using a div that has its ContentEditable property set to true then you can easily do this using:

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

您可以轻松设置此div的样式,使其看起来和感觉像文本区域.

You can easily style this div to look and feel like a textarea.

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

See jsFiddle here: http://jsfiddle.net/hqvDT/1/

这篇关于粗体显示“内部"一个文本区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆