[英]Making the text bold bugs it and sometimes it switches on and off
我知道标题不是很好,但我有一个div
,它的contenteditable="true"
基本上当按下 CTRL + B 时,您键入的文本应该是 go 粗体并且它可以工作。 但是当我打开它时,有时会打开和关闭粗体,这是它的一个片段:
<html> <head> <style>.textbox { border: 1px solid; border-radius: 5px; width: 50%; height: 170px; outnline: none; }.textbox: focus { border: 2px solid; } </style> </head> <body> <div onkeyup="boldText()" contenteditable="true" class="textbox"> </div> <script> function boldText() { if (event.keyCode == 17 + 66) { document.execCommand('bold'); } } </script> </body> </html>
好吧,首先, document.execCommand();
已弃用。 我会试试这个。
function boldText(e) { if (e.keyCode == 17 && e.keyCode == 66) { let textBox = document.querySelector('.textbox'); textBox.style.fontWeight = 'bold'; } } document.addEventListener('keyup', boldText);
.textbox { border: 1px solid; border-radius: 5px; width: 50%; height: 170px; outline: none; }.textbox: focus { border: 2px solid; }
<html> <body> <div contenteditable="true" class="textbox"> </div> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.