从CKEDITOR.dialog的onOk中插入iframe

Insert iframe in div from onOk of CKEDITOR.dialog

本文关键字:插入 iframe onOk CKEDITOR dialog      更新时间:2023-09-26

我使用CKEditor,当用户单击对话框中的OK按钮时,我在编辑器中的div元素中插入iframe有问题。这行不通。当用户单击按钮时,什么也没有发生(我没有错误消息)。所以他应该关闭弹出窗口在编辑器中插入一个包含iframe的div

你能帮我吗?

这是我的代码:

CKEDITOR.dialog。add('postVideoDialog', function(editor) {

return {
    title : 'Add Video',
    minWidth : 400,
    minHeight : 80,
    contents :
    [
        {
            id : 'video',
            label : 'Add Video',
            elements :
                [
                    {
                        type : 'text',
                        id : 'url',
                        label : 'Enter a URL from Vimeo :',
                        validate : function()
                                    {
                                        var url = this.getValue();
                                        var regex1=/^(http:'/'/)vimeo.com'/[0-9]{3,}$/g;
                                        var regex2=/^(http:'/'/)player.vimeo.com'/video'/[0-9]{3,}$/g;
                                        if(regex1.test(url) || regex2.test(url)){
                                            return true
                                        }else{
                                            alert("Url incorrect");
                                            return false;
                                        }
                                    },
                        required : true,
                        commit : function( data )
                        {
                            data.url = this.getValue();
                        }
                    },
                ]
        }
    ],
    onOk : function()
    {
        var dialog = this,
        data = {},
        iframe = editor.document.createElement( 'iframe' ),
        div = editor.document.createElement('div');
        this.commitContent( data );
        var regex=/^(http:'/'/)vimeo.com'/[0-9]{3,}$/g; //http://vimeo.com/25329849
        if(regex.test(data.url)){
            var idVideo = data.url.match(/[0-9]{3,}$/g);
            data.url = "http://player.vimeo.com/video/" + idVideo;
        }
        div.setAttribute('class', 'video');
        iframe.setAttribute( 'src', data.url + "?byline=0&portrait=0&color=ffffff");
        iframe.setAttribute( 'width', '620' );
        iframe.setAttribute( 'width', '349' );
        iframe.setAttribute( 'frameborder', '0');

        div.insertElement(iframe); //problem is here !
        editor.insertElement(div);
    }
}; });

找到了…

请阅读文档:docs.ckeditor.com/#!/api/CKEDITOR.dom.element

元素没有insertElement方法。这是编辑器的一个方法,试试这个:

    iframe.appendTo(div); //problem is solved here!
    editor.insertElement(div);

代替之前的代码:

    div.insertElement(iframe); //problem is here !
    editor.insertElement(div);