当我按下加粗按钮时,我希望能够加粗文本区域中的文本。但是,当我按下粗体按钮时,文本根本不变。此外,我希望文本在加粗后保持突出显示。我该怎么做呢?
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);
}发布于 2018-06-13 12:48:56

根据文档文档,execCommand仅为所选内容切换粗体开/关。如果您没有任何选择,则粗体样式将应用于插入点。
我想你不需要付出,
content.document.execCommand('bold',false,null);只是
document.execCommand('bold',false,null);就这么办。
https://stackoverflow.com/questions/50828853
复制相似问题