2010-06-21 19 views
5

我正在嘗試使用contenteditable和styleWithCss。webkit stylewithcss contenteditable不工作?

它似乎不適用於webkit。

無論何時我使用execCommand,它會生成一個<b>而不是我期望的範圍。

這裏有一個演示: http://jsbin.com/izomo/2/edit

文本的選擇部分,點擊大膽的按鈕,查看HTML輸出。

這是一個錯誤還是我做錯了什麼。

非常感謝。

回答

2

看起來好像一切都按預期工作。 請參閱WebKit BugZilla上的Bug 13490

編輯:支持styleWithCSS加入WebKit的源changeset 40560,03 2009年2月

這就是說,它好像從那以後,不管是什麼,styleWithCSS始終設置爲false,而在更改之前,樣式命令總是與CSS一起應用,就像存在styleWithCSS一樣,但始終設置爲true

我甚至嘗試重寫你的document.execCommand行,如下所示,按照Mozilla documentation

document.execCommand("styleWithCSS", true, null); 
document.execCommand("bold", false, null); 

這些修改的命令仍然工作在Firefox,而不是在任何瀏覽器5或Safari 5(包括Windows和安裝今天)。

所以,看起來這是一個WebKit錯誤。

+0

這對stylewithcss假,我想做stylewithcss真。 – Mark 2010-06-25 18:57:30

+0

D'oh!好的。我會潛水。 – 2010-06-25 22:58:18

+0

與原始版本相同的錯誤,但現在相反?經過一些更多的研究後,我更新了答案。我認爲'styleWithCSS'支持的增加應該是可配置的,但它仍然處於默認狀態,即'false'。 (見[Editor.cpp:940](http://trac.webkit.org/browser/trunk/WebCore/editing/Editor.cpp?rev=61418#L940)。) – 2010-06-26 01:19:45

1
document.execCommand("styleWithCSS", true, null); 
document.execCommand("bold", false, null); 
10

我無法得到這個在兩個答案這裏的命令工作。對於那些仍在爲這個問題感慨萬分的人們,這是如何讓它發揮作用。

我們可以通過三個值的execCommand

document.execCommand(command, uiElement, value) 

正如克里斯托弗的出色答卷詳細介紹了「styleWithCSS」的值默認 設置爲false只是嘗試:

alert(document.queryCommandState("styleWithCss")); 

要將其設爲如果您需要傳遞第三個參數「value」爲true,則爲true。就像這樣:

document.execCommand("styleWithCSS", null, true); 

這個工作在兩個Firefox和Chrome