我希望在按下粗体按钮时能够将文本区域中的文本加粗。但是,当我按下粗体按钮时,文本根本没有改变。此外,我希望文本在加粗后保持突出显示。我该怎么做呢?
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);
}
最佳答案
根据文档,execCommand 仅针对选择打开/关闭粗体。如果您没有任何选择,则粗体样式将应用于插入点。
我想你不需要付出,
content.document.execCommand('bold',false,null);
只是
document.execCommand('bold',false,null);
会的。
关于javascript - 所见即所得编辑器文本区域中的粗体文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50828853/