javascript TinyMCE 将原始 html 插入活动编辑器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29569078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:44:48  来源:igfitidea点击:

TinyMCE insert raw html into active editor

javascripttinymce

提问by swg1cor14

Using WindowManager for TinyMCE I open a window and it writes back raw HTML. But its truncating my links for my images. Whats weird is that it does NOT do it to the anchor tag. Just the image tag.

使用 WindowManager for TinyMCE 我打开一个窗口,它写回原始 HTML。但它截断了我的图片链接。奇怪的是它不会对锚标记执行此操作。只是图像标签。

I have this bit of code

我有这段代码

html = '<a title="'+ $('#title').val() +'" href="'+ $('#url').val() +'"><img src="'+ $('#imgURL').val() +'" /></a>';

    var parentEditor = parent.tinyMCE.activeEditor;
    parentEditor.execCommand('mceInsertRawHTML', false, html);
    parentEditor.windowManager.close();

It DOES insert the html into the active Editor. When I log htmlto the console I get

它确实将 html 插入到活动编辑器中。当我将html登录到控制台时,我得到

<a title="Click Action" href="yahoo.com"><img src="http://marketingedu.mychm.co/images/buttons/c2a-button4.png" /></a>

However when I view the Source Code in the tinyMCE editor, it changed the images SRC attribute to

但是,当我在 tinyMCE 编辑器中查看源代码时,它将图像 SRC 属性更改为

../../images/buttons/c2a-button4.png

Here is my entire javascript init for the TinyMCE editor

这是我的 TinyMCE 编辑器的整个 javascript init

tinymce.init({
        selector: ".editor",
        setup: function(ed) {
            ed.on('change', function(e) {
                tinyMCE.triggerSave();

                $('form').trigger('checkform.areYouSure');
            });
            ed.on('init', function(e) {
                autoresize_max_height: 500
            });
            ed.addButton('defaultbtn', {
                title: 'Insert Button',
                icon: 'fa fa-plus-square',
                onclick: function() {
                    // Open window
                    ed.windowManager.open({
                        title: 'Button Selector',
                        url: "<?=$this->url('/webinar/custombuttons') ?>",
                        width: 800,
                        height: 600
                    });
                }
            });
        },
        plugins: [
            "advlist autolink link responsivefilemanager lists charmap print preview hr anchor pagebreak spellchecker",
            "searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
            "save table contextmenu directionality template paste textcolor colorpicker responsivefilemanager autoresize"
        ],
        toolbar: "undo redo | styleselect | bold italic | forecolor backcolor | alignleft aligncenter alignright | bullist numlist | outdent indent | table | link responsivefilemanager defaultbtn",
        image_advtab: true ,

        external_filemanager_path:"/filemanager/",
        filemanager_title:"Filemanager" ,
        external_plugins: { "filemanager" : "/filemanager/plugin.min.js"},
    });

回答by revobtz

Change this:

改变这个:

var parentEditor = parent.tinyMCE.activeEditor;
parentEditor.execCommand('mceInsertRawHTML', false, html);

To This:

对此:

tinymce.activeEditor.setContent(html, {format: 'raw'});

Here is a reference linkon how to set content on tinymce in various of ways.

这是有关如何以各种方式在 tinymce 上设置内容的参考链接

Hope this helps.

希望这可以帮助。