document.queryCommandValue returns (empty string) in Firefox

早过忘川 提交于 2020-01-03 02:36:10

问题


I want to read if a certain text in a contenteditable element is bold or not. In Chrome document.queryCommandValue("bold") returns "true"/"false" as a string, IE returns a true/false as a boolean, but Firefox returns (empty string) in the developer console.

I made a fiddle as an example: http://jsfiddle.net/nTQd2/

If you write som text in the div, mark it and then hit "Bold" the span should show "true"/"false" or true/false. I don't really care if its as a string or a boolean as I can convert it later.


回答1:


For bold, italic and similar, use document.queryCommandState(). Use document.queryCommandValue() for non-Boolean commands.

http://jsfiddle.net/nTQd2/1/




回答2:


That's simple.

Document.queryCommandValue() 

returns a string. Instead of that use this.

Document.queryCommandState('Bold')

This will return a boolean value as true/false.

Example:

function isSelectedTextBold()
{
    var isBold = document.queryCommandState("Bold");
    return isBold;
}

I am also specifying function calls for other formatting options.

  1. Italic -> document.queryCommandState("Italic");
  2. Underline -> document.queryCommandState("Underline");
  3. Left Justify -> document.queryCommandState("justifyLeft");
  4. Center Justify -> document.queryCommandState("justifyCenter");
  5. Right Justify -> document.queryCommandState("justifyRight");

I hope this helps. :)



来源:https://stackoverflow.com/questions/20946596/document-querycommandvalue-returns-empty-string-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!