insertHTML无法在Firefox中使用某些标签,例如(a),(span)标签 [英] insertHTML not working with some tags in Firefox like (a), (span) tag

查看:70
本文介绍了insertHTML无法在Firefox中使用某些标签,例如(a),(span)标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试制作一个简单的Editor,但是一些Javascript编码无法在Firefox中使用,如:

Hi everybody,

I am trying to make an simple Editor but some Javascript cods not working with Firefox like:

document.getElementById(rteName).contentWindow.document.execCommand(‘insertHTML’, false, html);



insertHTML正在使用Firefox中的某些标签,例如<img>标签,但不适用于其他标签,例如:
<a>标签,<span>标签等...

任何建议请



insertHTML is working with some tags in Firefox like <img> tag but not work with others like:
<a> tag, <span> tag, ... etc.

any advice please

推荐答案

这适用于Web浏览器中的RTF编辑.

通常,我看到的几乎每个编辑器都将iframe用作编辑区域,如下所示:

-iframe:
This applies to Rich Text editing in web browsers.

Usually almost every editor I saw uses iframes as editing areas, something like this:

- the iframe:
<iframe id="editor" src="editable.html" style="width:500px;height:300px;border:solid 1px red;"></iframe>



-以及"editable.html"的内容:



- and the content for "editable.html":

<html>
<head>
    <title>Editable content example</title>
    <meta charset="utf-8" />
</head>
<body contenteditable="true">
    Some text in the editor.
</body>
</html>



-问题所需的脚本应与此类似(已在最新版本的firefox中进行过测试,但应从FF3开始使用):



- and the script you need for your problem should be something similar to this (tested in latest version of firefox, but should work from FF3 up):

<script type="text/javascript">
function InsertHtml() {
    var html = 'text for testing';

    var editor = document.getElementById("editor");
    var editorDoc;
    if (editor.contentDocument)
        editorDoc = editor.contentDocument;
    else
        editorDoc = editor.contentWindow.document;
    editorDoc.execCommand('insertHTML', false, html);
}
</script>



如果您将另一个HTML元素用作编辑器的包装器,例如DIV:



If you will use another HTML element as wrapper for your editor, like a DIV:

<div id="rteName" contentEditable="true" style="width:500px;height:300px;border:solid 1px red;">


并使用以下脚本或您的脚本,您将获得未定义的"contentWindow".
我真的不知道是什么原因,但这就是它的工作原理.

另外,在Chrome中,只有当您在编辑器中时(编辑器区域处于活动状态),它才起作用.

希望对您有帮助,
Valy.


and use the following script or your script, you will get "contentWindow" is undefined.
I don''t really know the reason for this, but this is how it works.

And another thing, in Chrome it only works if you are inside the editor (the editor area is active).

Hope it helps,
Valy.


这篇关于insertHTML无法在Firefox中使用某些标签,例如(a),(span)标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆