如何预防| Mozilla FireFox (3.6) ContentEditable -- 将 CSS 应用到可编辑容器而不是其内容
我有一些页面是这样的:
<div id="editor" contenteditable="true">SomeText</div>
我有一个自制的 JS 编辑器,
document.execCommand(some_command,false,optional_value);
当用户按下编辑器中的按钮时,它实际上会发出问题。 (例如我有简单的[粗体]按钮)。
只要我对“SomeText”的一部分进行编辑,一切就都很好。例如,用鼠标选择“Text”并按 [Bold] 按钮(这会导致 document.execCommand("bold",false,false);)将产生:
<div id="editor" contenteditable="true">Some<span style="some-css-here">Text</span></div>
但是当我选择 div 的整个内容时(此中的“SomeText”)例如)并在我的编辑器中按 [Bold],FF 不会产生预期的结果
<div id="editor" contenteditable="true"><span style="some-css-here">SomeText</span></div>
,而是
<div id="editor" contenteditable="true" style="some-css-here">SomeText</div>
注意“style”属性进入了可编辑的 div!
为什么这对我有影响? --这是因为编辑完成后,我想获取可编辑 div 的内容以及所有样式、格式等,并在页面上进一步使用它。但我不能——所有的样式现在都位于 div 内。 当我被建议从 div 中提取样式时,解决方案是不可接受的 - div 在其生命周期中从页面的其他活动元素中获取大量样式(大量使用 jQuery)
简单来说: 如何告诉 FF 永远不要碰可编辑的 div 并将所有样式仅应用于其内部内容?
真诚感谢您的宝贵时间。 (刚刚扯下我最后的头发,与许多其他人一起浏览 FF 开发网站(((( )
I have some page with something like this:
<div id="editor" contenteditable="true">SomeText</div>
I have an selfmade JS editor which actually issues
document.execCommand(some_command,false,optional_value);
when user presses a button in the editor. (For example I have plain, simple [Bold] button).
Everything is fine as long as I apply editing to part of "SomeText". For example selecting "Text" with mouse and pressing [Bold] button (which leads to document.execCommand("bold",false,false);) will produce:
<div id="editor" contenteditable="true">Some<span style="some-css-here">Text</span></div>
but when I select entire content of the div ("SomeText" in this example) and press [Bold] in my editor, FF will not produce expected
<div id="editor" contenteditable="true"><span style="some-css-here">SomeText</span></div>
but rather
<div id="editor" contenteditable="true" style="some-css-here">SomeText</div>
Notice the "style" attribute went into the editable div!
Why this makes a difference to me?
--It's because after editing is done I would like to take the content of the editable div, along with all styles, formating etc and further use it on the page. But I can't -- all the styling now sits inside the div.
A solution when I would be advised to extract styles from the div is not acceptable -- the div during its life takes a lot of styles from other active elements of the page (heavy jQuery usage)
So in brief:
How to tell FF to never touch editable div and apply all styling to its inner contents only?
Sincere thanks for you time.
(just pulled last of my hair, browsing FF dev site along with many others(((( )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在任何其他 execCommand 之前调用一次并将 FF 切换到标签模式
Call once before any other execCommand and switch FF to tag mode
有时整理和写下我的想法会给我带来非常积极的结果。
我已经找到了满意的解决方案。
1)将隐藏的 div 作为第一个子节点插入到您的编辑 div 中:
2)它的 CSS:
3)现在您可以编辑。我用这个小测试测试了它(实际上我需要所有这些东西来编辑简短的文本,例如标题,新闻主题等)
4)在使用内容之前 - 显而易见 - 删除该 div。
5)当您想返回编辑时——再次插入。
来自工作(最后!)))项目的一些代码:
当然 - 测试待定。
我想听听你的消息;)
问候。
Sometimes organizing and writing my thoughts brings me very positive results.
I have found satisfactory solution.
1)insert hidden div as a first child node into your editing div:
2) The CSS for it:
3)Now you can edit. I tested it with this my small test (actually I need all this stuff to edit short pieces of text like like captions, news subjects etc)
4)Before you use the content -- obious -- remoe that div.
5)When you want to return to editing -- insert it again.
Some bits of code from working (finally! ))) project:
of course -- testing pending.
I would like to hear from you ;)
regards.
我遇到了类似的问题,例如,当用鼠标选择 contenteditable 区域中的所有内容或使用 CTRL-A 然后按 CTRL+B 时,Firefox 将样式放入 contenteditable 容器而不是内容。
同样适用于斜体、字体大小、字体系列和其他内联样式。
我写了一个函数来解决这个问题。它在内容下方创建新元素并更改所选范围直到该元素:
此代码仅影响 FF,对于其他浏览器,它将仅应用 execCommand
I had the similar problem, when select all in contenteditable area with mouse or use CTRL-A there and then press CTRL+B for example, Firefox put style to the contenteditable container instead it's content.
Same applyed for italic, font size, font-family and other inline styles.
I wrote a function which fixing that issue. It creates new element below the content and changes selected range till that element:
this code affects only FF, for other browsers it will just apply execCommand