WYSIWYG编辑器TextArea中的粗体文本

时间:2018-06-13 03:55:45

标签: 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);
}

1 个答案:

答案 0 :(得分:0)

enter image description here

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

我想你不需要给予,

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

只是

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

会做的。