简体   繁体   English

在 IE 的自定义标签中使用 innerHTML

[英]using innerHTML at custom tag in IE

I have a problem I just can't solve, and need your advice since I'm out of ideas:我有一个我无法解决的问题,由于我没有想法,需要你的建议:

Context: I'm using tinyMCE Editor on my website and developed a custom plugin to include external xml files.背景:我在我的网站上使用 tinyMCE 编辑器并开发了一个自定义插件以包含外部 xml 文件。 So far everything works as expected.到目前为止,一切都按预期工作。 The links to the external xml files are represented as span-Tags:外部 xml 文件的链接表示为跨度标记:

<span id="-[XML Document 1]-" title="erg" class="xml_embed xml_include">-[XML Document 1]-</span>

but only in the tinyMCE editor with a custom class (xml_include) to distinguish them from normal text and upon switching to the html/source code view or saving, those span tags get replaced to xi:include elements:但仅在 tinyMCE 编辑器中使用自定义 class (xml_include) 以将它们与普通文本区分开来,并且在切换到 html/源代码视图或保存时,这些 span 标签将替换为 xi:include 元素:

<xi:include xmlns:xi="http://www.w3.org/TR/XInclude" show="xml_embed" href="erg">-[XML Document 1]-</xi:include>

The text that was set as innerHTML ("-XML Document 1]-") for the span tag(s) serves as placeholder in the editor and gets moved to the xi:include tag(s) in the source view and serves as placeholder there also.为 span 标记设置为 innerHTML(“-XML 文档 1]-”)的文本在编辑器中用作占位符,并在源视图中移动到 xi:include 标记并用作占位符还有。


Now to the problem:现在解决问题:

The code to transform span.xml_include to xi:include gets called before the source code popup is displayed:在显示源代码弹出窗口之前调用将span.xml_include转换为xi:include的代码:

ed.onPreProcess.add(function(ed, o) {
var elm;
var domelm;
//get all span.xml_include elements
tinymce.each(ed.dom.select('span.xml_include', o.node), function(n) {
//IE ignores innerHTML when created with tinymce.dom, therefore use native JS createElement method to tell IE that custom tag is valid HTML
    if(tinymce.isIE)
    {
        domelm = document.createElement('xi:include');
        domelm.setAttribute("xmlns:xi", "http://www.w3.org/TR/XInclude");
        domelm.href = n.title;
        domelm.innerHTML = n.innerHTML;
        domelm.show = n.className.split(/\s+/)[0];
        document.body.appendChild(domelm);
        ed.dom.replace(domelm, n);
    }
    else
    {
        //ed = tinyMCE.activeEditor
        elm = ed.dom.create('xi:include', {href: n.title, show: n.className.split(/\s+/)[0]}, n.innerHTML);
        elm.setAttribute("xmlns:xi", "http://www.w3.org/TR/XInclude");
        ed.dom.replace(elm, n);
    }
  });
});

this code works perfectly fine in FF and Chrome, but not in IE (I tested 7 & 8): in IE the innerHTML of the new element "domelm" can't be set.此代码在 FF 和 Chrome 中运行良好,但在 IE 中无法正常工作(我测试了 7 和 8):在 IE 中,无法设置新元素“domelm”的 innerHTML。 Either it stays blank or if set explicitly an error is thrown.它要么保持空白,要么如果明确设置,则会引发错误。 n.innerHTML can be accessed. n.innerHTML 可以访问。 I get an "Unknown runtime error" for the line domelm.innerHTML = n.innerHTML;我收到domelm.innerHTML = n.innerHTML;

What else did I try?我还尝试了什么?

  • the native JS way: domelm.appendChild(document.createTextNode(n.innerHTML));原生JS方式: domelm.appendChild(document.createTextNode(n.innerHTML)); to create a text node and append it to the "domelm" with no success (getting error: "unexpected call to method or property access", that should be the translation from "Unerwarteter Aufruf oder Zugriff" (german version))创建一个文本节点和 append 到“domelm”,但没有成功(出现错误:“意外调用方法或属性访问”,应该是“Unerwarteter Aufruf oder Zugriff”(德语版)的翻译)

  • the tinyMCE API way: tinymce.DOM.setHTML(domelm, n.innerHTML); tinyMCE API 方式: tinymce.DOM.setHTML(domelm, n.innerHTML); resulted in no error but also the usual blank innerHTML导致没有错误,但也是通常的空白 innerHTML

  • the jQuery way: $('#domelm').html(n.innerHTML); jQuery 方式: $('#domelm').html(n.innerHTML); or first var jQelm = $(domelm);或第一个var jQelm = $(domelm); then jQelm.text(...);然后jQelm.text(...); or jQelm.html(...);jQelm.html(...); doesn't matter, neither works, IE always returns "unexpected call to method" error in the jquery core, which I obviously won't touch..没关系,两者都不起作用,IE总是在jquery核心中返回“意外调用方法”错误,我显然不会碰它。

  • the tinyMCE way of creating elements as seen in the "else" part of the if condition above..if domelm.innerHTML = n.innerHTML; tinyMCE 创建元素的方式如上面 if 条件的“else”部分所示..if domelm.innerHTML = n.innerHTML; isn't explicitly set, elm.innerHTML just stays blank, else the same errors as on the approaches above occur, therefore I could as well skip the if(tinymce.isIE) detection..没有明确设置,elm.innerHTML 只是保持空白,否则会发生与上述方法相同的错误,因此我也可以跳过if(tinymce.isIE)检测..

What else can I do?我还可以做些什么? Suggestions?建议?

I also made sure to properly declare the custom xml namespaces, changed the MIME-type to application/xhtml+xml instead of simply text/html , "announced" the xi:include node for IE with document.createElement('xi:include');我还确保正确声明自定义 xml 命名空间,将 MIME 类型更改为application/xhtml+xml而不是简单的text/html ,使用document.createElement('xi:include'); “宣布”IE 的xi:include节点document.createElement('xi:include'); and generally changed the code to please IE..and this seems to be the last major bug I have to overcome.. I'm not sure if it's an error in my code since FF and Chrome work fine local and remote and don't show any errors..?并且通常将代码更改为取悦 IE ..这似乎是我必须克服的最后一个主要错误..我不确定这是否是我的代码中的错误,因为 FF 和 Chrome 在本地和远程都可以正常工作并且不显示任何错误..?

Any help is appreciated, I hope I provided enough context for you so that it's clear what I meant.感谢您提供任何帮助,我希望我为您提供了足够的背景信息,以便清楚我的意思。 and sorry for mistakes, English is not my first language:)对不起,英语不是我的第一语言:)

Ok, wrapping the custom element in ap/div/span tag finally did the trick: I used span to leave the formatting unmodified..here is what I did:好的,将自定义元素包装在 ap/div/span 标记中终于成功了:我使用 span 使格式保持不变..这是我所做的:

In the "if(tinymce.isIE) part of the onPreProcess function, before "xi:include" is created, a wrapper is needed:在 onPreProcess function 的“if(tinymce.isIE)”部分中,在创建“xi:include”之前,需要一个包装器:

var wrapper = document.createElement('span');

Appending the custom tag-element to the wrapper:将自定义标签元素附加到包装器:

wrapper.appendChild(domelm);

and appending a textNode to the wrapper since appending it to the domelm throws errors:并将 textNode 附加到包装器,因为将其附加到 domelm 会引发错误:

wrapper.appendChild(document.createTextNode(n.innerHTML));

and finally append the wrapper to the dom and replace the "span" tag (n) with the wrapped "xi:include" (wrapper, span tag to be modified):最后 append 将包装器包装到 dom 并用包装的“xi:include”替换“span”标签(n)(包装器,要修改的span标签):

document.body.appendChild(wrapper);
ed.dom.replace(wrapper, n);`

The result is a custom "xi:include" tag in IE with the correct innerHTML:结果是在 IE 中使用正确的 innerHTML 自定义“xi:include”标签:

<span><xi:include xmlns:xi="http://www.w3.org/TR/XInclude" href="eh" show="xml_embed">-[XML Document]-</xi:include></span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM