首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >所见即所得编辑器TextArea中的粗体文本

问所见即所得编辑器TextArea中的粗体文本
EN

Stack Overflow用户
提问于 2018-06-13 11:55:45
回答 1查看 596关注 0票数 0

当我按下加粗按钮时,我希望能够加粗文本区域中的文本。但是,当我按下粗体按钮时,文本根本不变。此外,我希望文本在加粗后保持突出显示。我该怎么做呢?

HTML代码:

代码语言:javascript
复制
 <div class="col-md-8">
                            <input type = "button" value = "B" onclick = "bold()">
                            <input type = "button" value = "I" onclick = "italics()">
                            <input type = "button" value = "U" onclick = "underline()">
                            <input type = "button" value = "Size" onclick = "fontsize()">
                            <input type = "button" value = "Color" onclick = "fontcolor()">
                            <input type = "button" value = "Highlight" onclick = "highlight()">
                            <input type = "button" value = "Link" onclick = "link()">
                            <input type = "button" value = "Strike" onclick = "strikethrough()">
                            <input type = "button" value = "Exp" onclick = "exponent()">
                            <input type = "button" value = "Sub" onclick = "subscript()">
                            <input type = "button" value = "Bullet" onclick = "bullet()">
                            <input type = "button" value = "List" onclick = "list()">
                            <input type = "button" value = "Space" onclick = "spacing()">
                            <br><br>

                        <textarea name = "content" id = "content" style = "width: 720; height: 400;" ></textarea>

Javascript代码:

代码语言:javascript
复制
function bold(){
    content.document.execCommand('bold', false, null);
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-13 12:48:56

​

​

根据文档文档,execCommand仅为所选内容切换粗体开/关。如果您没有任何选择,则粗体样式将应用于插入点。

我想你不需要付出,

代码语言:javascript
复制
content.document.execCommand('bold',false,null);

只是

代码语言:javascript
复制
document.execCommand('bold',false,null);

就这么办。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50828853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档