Document.queryCommandState()

非推奨: この機能は非推奨になりました。まだ対応しているブラウザーがあるかもしれませんが、すでに関連するウェブ標準から削除されているか、削除の手続き中であるか、互換性のためだけに残されている可能性があります。使用を避け、できれば既存のコードは更新してください。このページの下部にある互換性一覧表を見て判断してください。この機能は突然動作しなくなる可能性があることに注意してください。

queryCommandState() メソッドは、現在の選択範囲に特定の Document.execCommand() コマンドが適用されているかどうかを知らせます。

構文

js
queryCommandState(String command)

引数

commandDocument.execCommand() のコマンドです。

返値

queryCommandState() は論理値、または状態が不明な場合は null を返す可能性があります。

HTML

html
<div contenteditable="true">Select a part of this text!</div>
<button onclick="makeBold();">Test the state of the 'bold' command</button>

<hr />

<div id="output"></div>

JavaScript

js
function makeBold() {
  const state = document.queryCommandState("bold");
  let message;
  switch (state) {
    case true:
      message = "The bold formatting will be removed from the selected text.";
      break;
    case false:
      message = "The selected text will be displayed in bold.";
      break;
    default:
      message = "The state of the 'bold' command is indeterminable.";
      break;
  }
  document.querySelector("#output").textContent = `Output: ${message}`;
  document.execCommand("bold");
}

結果

仕様書

この機能は、現在のどの仕様にも含まれていません。標準化される予定もありません。

ブラウザーの互換性

BCD tables only load in the browser

関連情報