我已经创建了HTML和javascript文件。我在fontawesome.com中添加了一个粗体图标,并使用该图标制作了一个按钮。我使用execCommand尝试使加粗按钮在单击按钮后在文本区域中编辑选定的文本。但是代码不起作用。
<html>
<head>
<title>
Editor
</title>
<link rel="stylesheet" href="stylesheet.css">
<script src="script.js"> </script>
<script src="https://kit.fontawesome.com/f7ac85e141.js" crossorigin="anonymous"></script>
</head>
<body>
<header>
<div class="navBar" id="heading-container" contentEditable="true">
<button onclick="execCmd('bold');">
<i class="fas fa-bold"></i>
</button>
</div>
</header>
<textarea id="myTextArea"> Lorem ipsum dolor sit amet,consectetur. </textarea>
</body>
</html>
function execCmd(command) {
let field = document.getElementById('myTextArea');
field.document.execCommand(command,false,null);
}