[英]Is there a way to determine if execCommand('bold') is being executed right now?
我正在構建一個帶有粗體功能的簡單JavaScript文本編輯器。 我為此使用execCommand('bold')
。 對於工具欄中的粗體圖標,我必須檢查我現在是否正在寫粗體。 我想到的是每次執行命令時都會設置的布爾變量,但這是不可能的,因為可以選擇文本,依此類推。
那么有沒有辦法確定execCommand('bold')是否正在執行?
您需要Document.queryCommandState()方法。
bolder.onclick = e => { document.execCommand('bold'); }; inp.onfocus = document.onselectionchange = e => bolder.classList.toggle('active', isWritingBold()); function isWritingBold() { return document.queryCommandState('bold'); }
#bolder.active::before { content: 'un-'; }
<button id="bolder">boldify current selection</button> <div id="inp" contenteditable>edit me and <b>boldify</b> any part of my content</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.