2012-06-27 17 views
1

我已經構建了一個帶有iframe和designMode的小型文本編輯器。 (這裏是一個例子,不用我編碼http://jsfiddle.net/Kxmaf/6/。我在Stackoverflow上找到它)。帶有designMode的文本編輯器。如何強制designMode使用HTML標記而不是跨度

如果我檢索iframe的內容(document.body.innerHTML)的結果是這樣的:<span style="font-weight: bold;">Test</span> Text <span style="font-style: italic;">Text</span> <span style="text-decoration: underline;">TT</span>

是否有可能設置爲使用標籤,而不是跨越風格屬性的designMode?

任何幫助是非常讚賞:)

回答

1

在大多數瀏覽器(但不是IE),您可以使用「StyleWithCSS」命令式的模式之間切換,讓您使用的元素,例如<b>造型之間進行選擇和<i>或使用樣式屬性使用<span>元素的樣式。您可以使用「StyleWithCSS」命令執行此操作,並回退到舊版瀏覽器中的「UseCSS」。以下開關命令使用非CSS版本:

try { 
    if (!document.execCommand("StyleWithCSS", false, useCss)) { 
     // The value required by UseCSS is the inverse of what you'd expect 
     document.execCommand("UseCSS", false, !useCss); 
    } 
} catch (ex) { 
    // IE doesn't recognise these commands and throws. 
} 
相關問題