2014-01-06 51 views
1

我想閱讀如果contenteditable元素中的某個文本是否爲粗體。在Chrome中,document.queryCommandValue("bold")返回"true"/"false"作爲字符串,IE返回true/false作爲布爾值,但Firefox在開發者控制檯中返回(empty string)document.queryCommandValue在Firefox中返回(空字符串)

我做了一個小提琴爲例: http://jsfiddle.net/nTQd2/

如果你寫在div索姆文本,標記它,然後點擊「大膽」的跨度應該顯示"true"/"false"true/false。我真的不在乎它作爲一個字符串或布爾值,因爲我可以稍後再進行轉換。

回答

0

這很簡單。

Document.queryCommandValue() 

返回一個字符串。而不是用這個。

Document.queryCommandState('Bold') 

這將返回布爾值爲true/false。

例子:

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

我也指定函數調用其他格式化選項。

  1. 斜體 - > document.queryCommandState(「Italic」);
  2. 下劃線 - > document.queryCommandState(「Underline」);
  3. Left Justify - > document.queryCommandState(「justifyLeft」);
  4. Center Justify - > document.queryCommandState(「justifyCenter」);
  5. Right Justify - > document.queryCommandState(「justifyRight」);

我希望這會有所幫助。 :)