javascript - 所见即所得编辑器文本区域中的粗体文本

标签 javascript html wysiwyg text-editor

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

HTML 代码:

 <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代码:

function bold(){
    content.document.execCommand('bold', false, null);
}

最佳答案

enter image description here

根据文档,execCommand 仅针对选择打开/关闭粗体。如果您没有任何选择,则粗体样式将应用于插入点。

我想你不需要付出,

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

只是

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

会的。

关于javascript - 所见即所得编辑器文本区域中的粗体文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50828853/

相关文章:

wysiwyg - 如何将 CKFinder 与 CKEditor 集成?

javascript - 设置心电图纸质网格间隔(highcharts.js)

javascript - 如何从渲染的 html 将初始数据添加到 angularjs 应用程序

javascript - 使用 JavaScript 更改 IE 中的背景图像

html - Facebook 喜欢的框边距不居中

javascript - execCommand() 将 url 替换为 ../

html - 所见即所得的 HTML/CSS 编辑器,可以生成 div、float、padding

php - 将 php var 传递给 javascript

javascript - AngularJS 自定义 $http 服务,具有自动重试功能

javascript - 开源客户端购物车 - jQuery/Cookies