如何预防| Mozilla FireFox (3.6) ContentEditable -- 将 CSS 应用到可编辑容器而不是其内容

发布于 2024-10-05 17:18:30 字数 1194 浏览 0 评论 0原文

我有一些页面是这样的:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尽揽少女心 2024-10-12 17:18:30

在任何其他 execCommand 之前调用一次并将 FF 切换到标签模式

document.execCommand('StyleWithCSS', false, false);

Call once before any other execCommand and switch FF to tag mode

document.execCommand('StyleWithCSS', false, false);
夏の忆 2024-10-12 17:18:30

有时整理和写下我的想法会给我带来非常积极的结果。

我已经找到了满意的解决方案。

1)将隐藏的 div 作为第一个子节点插入到您的编辑 div 中:

<div id="editor" contenteditable="true">
   <div class="edit_text_mozilla_hack"></div>
   SomeText
</div>

2)它的 CSS:

.edit_text_mozilla_hack {
    display: block;
    width: 0;
    height: 0;
    -moz-user-edit: none;
    -moz-user-select: none
}

3)现在您可以编辑。我用这个小测试测试了它(实际上我需要所有这些东西来编辑简短的文本,例如标题,新闻主题等)

4)在使用内容之前 - 显而易见 - 删除该 div。
5)当您想返回编辑时——再次插入。

来自工作(最后!)))项目的一些代码:

//adds hidden div to all editable regions 'editables'
//the parameter is for speeding the thins up -- I'm often working with all or a lot of editable regions
function editAddMozillaHack(editables) {
    if (!editables) {
        editables = editGetEditables();
    }
    $("." + adminOptions["admin_loader"]).remove();
    editables.each(function() {
        $(this).prepend('<div class="edit_text_mozilla_hack"></div>')
    });
}

//removes the hack from all regions
function editRemoveMozillaHack() {
    $(".edit_text_mozilla_hack").remove();
}

//just returns all the editable regions -- my project often requires them all
function editGetEditables() {
    return $("[contenteditable=\"true\"]");
}

当然 - 测试待定。
我想听听你的消息;)
问候。

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:

<div id="editor" contenteditable="true">
   <div class="edit_text_mozilla_hack"></div>
   SomeText
</div>

2) The CSS for it:

.edit_text_mozilla_hack {
    display: block;
    width: 0;
    height: 0;
    -moz-user-edit: none;
    -moz-user-select: none
}

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:

//adds hidden div to all editable regions 'editables'
//the parameter is for speeding the thins up -- I'm often working with all or a lot of editable regions
function editAddMozillaHack(editables) {
    if (!editables) {
        editables = editGetEditables();
    }
    $("." + adminOptions["admin_loader"]).remove();
    editables.each(function() {
        $(this).prepend('<div class="edit_text_mozilla_hack"></div>')
    });
}

//removes the hack from all regions
function editRemoveMozillaHack() {
    $(".edit_text_mozilla_hack").remove();
}

//just returns all the editable regions -- my project often requires them all
function editGetEditables() {
    return $("[contenteditable=\"true\"]");
}

of course -- testing pending.
I would like to hear from you ;)
regards.

漆黑的白昼 2024-10-12 17:18:30

我遇到了类似的问题,例如,当用鼠标选择 contenteditable 区域中的所有内容或使用 CTRL-A 然后按 CTRL+B 时,Firefox 将样式放入 contenteditable 容器而不是内容。

<div contenteditable="true" style="font-weight: bold;"><p>..content..</p></div>

同样适用于斜体、字体大小、字体系列和其他内联样式。
我写了一个函数来解决这个问题。它在内容下方创建新元素并更改所选范围直到该元素:

function checkSelectAll (container, cmd, args) {
    if(document.getSelection) {
        var     cn = container.childNodes,
            s = document.getSelection(),
            r = s.getRangeAt(0);

        if(r.startContainer == container && r.endContainer == container){
            var endMarker = document.createElement('SPAN')
            container.appendChild(endMarker);
            r.setEndBefore(endMarker);
            s.removeAllRanges();
            s.addRange(r);
            document.execCommand(cmd,false,args);
            container.removeChild(endMarker);

        } else {
            document.execCommand(cmd,false,args);
        }
    } else {
        document.execCommand(cmd,false,args);
    }
};

此代码仅影响 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.

<div contenteditable="true" style="font-weight: bold;"><p>..content..</p></div>

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:

function checkSelectAll (container, cmd, args) {
    if(document.getSelection) {
        var     cn = container.childNodes,
            s = document.getSelection(),
            r = s.getRangeAt(0);

        if(r.startContainer == container && r.endContainer == container){
            var endMarker = document.createElement('SPAN')
            container.appendChild(endMarker);
            r.setEndBefore(endMarker);
            s.removeAllRanges();
            s.addRange(r);
            document.execCommand(cmd,false,args);
            container.removeChild(endMarker);

        } else {
            document.execCommand(cmd,false,args);
        }
    } else {
        document.execCommand(cmd,false,args);
    }
};

this code affects only FF, for other browsers it will just apply execCommand

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文