1. 開発者向けのウェブ技術
  2. Web API
  3. Document
  4. queryCommandState()

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

Document: queryCommandState() メソッド

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

非標準: この機能は標準化されていません。非標準の機能はブラウザーの対応が限られ、将来的に変更または削除される可能性があるため、本番環境での使用は推奨されません。ただし、標準の選択肢が存在しない特定のケースでは、有効な代替手段となる場合があります。

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

構文

js
queryCommandState(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>
hr,
button {
 margin: 1rem 0;
}

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");
}

結果

仕様書

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

ブラウザーの互換性

関連情報

MDN の改良に協力

協力方法を知る

このページは MDN の貢献者によって に最終更新されました。

AltStyle によって変換されたページ (->オリジナル) /