【问题标题】:Caret not shown in empty iframe插入符号未显示在空 iframe 中
【发布时间】:2016-06-23 04:15:04
【问题描述】:

我正在尝试使用 iframe 实现我自己的富文本编辑器。但是,当我在空的富文本编辑器中单击时,插入符号或鼠标光标不会出现。在google上查了一下,发现我们需要事先在iframe中添加一些html,比如'break tag.但是我下面的代码只会将中断标记添加到第一个富文本编辑器而不是第二个。不知道我在这里做错了什么......

$.fn.createRichTextEditor = function(width,height="auto") {
    var iframeDocument;

    var iframeDocumentId = this.attr("id") + "-rte-editor";

    var newHtml =   "<div id='rte-" + iframeDocumentId + "' class='rte'>" +
                    "<ul class='rte-toolbar'>" +
                    "<li id='bold'><button id='rte-boldBtn-" + iframeDocumentId + "' class='rte-button' value='bold' title='Bold'></button></li>" +
                    "<li id='italic'><button id='rte-italicBtn-" + iframeDocumentId + "' class='rte-button' value='italic' title='Italic'></button></li>" +
                    "<li id='underline'><button id='rte-underlineBtn-" + iframeDocumentId + "' class='rte-button' value='underline' title='Underline'></button></li>" +
                    "<li id='createLink'><button id='rte-createLinkBtn-" + iframeDocumentId + "' class='rte-button-link' value='createLink' title='Link'></button></li>" + 
                    "<li id='unlink'><button id='rte-unlinkBtn-" + iframeDocumentId + "' class='rte-button-link' value='unlink' title='Unlink'></button></li>" + 
                    "</ul>" +
                    "<iframe class='rte-iframe' id='" + iframeDocumentId + "' frameBorder='0'></iframe>" +
                    "</div>";

    this.after(newHtml);
    this.css('display', 'none');

    var iframe = document.getElementById(iframeDocumentId);
    var rte = iframe.parentElement;
    $(rte).css('width', width);
    $(rte).css('border', '1px solid #ccc');

    $(iframe).on('load', function() {
        $(iframe).width("100%");
        $(iframe).height(height);
        iframeDocument = iframe.contentDocument;    
        iframeDocument.designMode = 'On';
        $(iframeDocument).find('body').html('<br><br/>');

    });

};

【问题讨论】:

  • 您不需要 load 事件,因为 iframe 没有源 + 您需要专注于 iframe 窗口。这是一个工作演示:https://jsfiddle.net/j1p2m692/2/
  • 不起作用。如果删除负载,甚至无法编辑任何内容
  • 确实,它在 Firefox 上不起作用!这是另一个例子,同时适用于 Chrome 和 Firefox https://jsfiddle.net/d98mhv0s/
  • @IsmailRBOUH 这很有帮助。你能把这个作为答案,所以我可以投票并选择作为答案吗?

标签: javascript jquery iframe


【解决方案1】:

您需要遍历插件中的每个匹配元素:

$.fn.createRichTextEditor = function (width, height) {

    this.each(function () {

        // Your code for each element here

    });

});

【讨论】:

    猜你喜欢
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    相关资源
    最近更新 更多