最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - webkit stylewithcss contenteditable not working? - Stack Overflow

programmeradmin3浏览0评论

I'm trying to use contenteditable and styleWithCss.

It doesn't seem to work in webkit.

Anytime I use do an execCommand, it generates a <b> rather than the span I expected.

Here's a demo:

Select part of the text, click the bold button, and see the html output.

Is this a bug or am I doing something wrong.

Thank you very much.

I'm trying to use contenteditable and styleWithCss.

It doesn't seem to work in webkit.

Anytime I use do an execCommand, it generates a <b> rather than the span I expected.

Here's a demo: http://jsbin.com/izomo/2/edit

Select part of the text, click the bold button, and see the html output.

Is this a bug or am I doing something wrong.

Thank you very much.

Share Improve this question edited Jun 25, 2010 at 18:22 Mark asked Jun 21, 2010 at 22:39 MarkMark 33.6k34 gold badges108 silver badges139 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 10

I was unable to get this to work with the commands in both the answers here. For those who are still banging their heads with the issue this is how to get it to work.

We can pass three values to execCommand

document.execCommand( command, uiElement, value ) 

As Christopher's excellent Answer details the value of "styleWithCSS" is set to false by default just try:

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

To set it to true you need to pass the third argument, "value" as true. Like this:

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

This works in both Firefox and Chrome

It seems like everything is working as intended. See Bug 13490 on WebKit BugZilla.

EDIT: Support for styleWithCSS was added to WebKit's source in changeset 40560, on 03 February 2009.

That said, it seems like ever since then, no matter what, styleWithCSS is always set to false, whereas before the change, style commands were always applied with CSS, as if styleWithCSS existed but was always set to true.

I even tried rewriting your document.execCommand line as follows, in accordance with the Mozilla documentation:

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

These modified commands still work in Firefox, but not in either Chrome 5 or Safari 5 (both on Windows and installed today).

So, it would seem like this is a WebKit bug.

document.execCommand("styleWithCSS", true, null);
document.execCommand("bold", false, null);
发布评论

评论列表(0)

  1. 暂无评论