[英]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.