问题
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.
- Italic -> document.queryCommandState("Italic");
- Underline -> document.queryCommandState("Underline");
- Left Justify -> document.queryCommandState("justifyLeft");
- Center Justify -> document.queryCommandState("justifyCenter");
- Right Justify -> document.queryCommandState("justifyRight");
I hope this helps. :)
来源:https://stackoverflow.com/questions/20946596/document-querycommandvalue-returns-empty-string-in-firefox