Document.queryCommandEnabled()

注意

该方法在部分浏览器返回的结果是不可预料的。因此,建议使用execCommand的返回值直接判断,或通过其它方式嗅探,而非使用该方法。

Document.queryCommandEnabled() 方法可查询浏览器中指定的编辑指令是否可用。

语法

var isEnabled = document.queryCommandEnabled(command);

参数

command
待查询的是否可用的指令.

返回值

返回 Boolean 值,true 表示指令可用,false表示指令不可用

备注

  • 经过测试,在部分浏览器它永远返回 false,而IE浏览器即使对于同样支持的属性也可能有不同返回值;有时IE还会对不支持的属性抛出异常而不是返回 false
  • 对于 "cut" 和 "copy" 指令,只有当用户启动的线程调用该方法时才返回true。
  • "paste" 指令不仅当特性不可用时返回 false ,脚本权限不足时也一样。

示例

var flg = document.queryCommandEnabled("SelectAll");

if(flg) {
  document.execCommand("SelectAll", false, null); // command is enabled, run it
}

规范

说明 状态 描述
HTML Editing APIs
querycommandenabled
Editor's Draft Initial definition

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
queryCommandEnabled
DeprecatedNon-standard
Chrome Full support 1Edge Full support 12Firefox Full support 41
Full support 41
No support 9 — 41
Notes
Notes queryCommandEnabled with arguments cut, copy or paste would throw errors unless the script had special privileges.
IE Full support 4Opera Full support ≤12.1Safari Full support 2WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 41
Full support 41
No support 9 — 41
Notes
Notes queryCommandEnabled with arguments cut, copy or paste would throw errors unless the script had special privileges.
Opera Android Full support ≤12.1Safari iOS Full support 1Samsung Internet Android Full support 1.0

Legend

Full support  
Full support
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.

参考资料