最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - TinyMCE, insert HTML into specific TinyMCE instance - Stack Overflow

matteradmin5PV0评论

I have multiple TinyMCE editors on one page and I need to insert HTML in a specific one, currently when I insert HTML, it inserts it into the last one:

My init code:

<script type="text/javascript">
$(document).ready(function() {

    // enable tinyMCE on shortDescription
    tinymce.init({
        mode : "exact",
        elements :"shortDescriptionHtml",
        plugins: [
            "advlist autolink lists link image charmap print preview anchor",
            "searchreplace visualblocks code fullscreen",
            "insertdatetime media table contextmenu paste"
        ],
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
        relative_urls : false,
        remove_script_host : false
    });

    // enable tinyMCE on fullDescription
    tinymce.init({
        mode : "exact",
        elements :"fullDescriptionHtml",
        plugins: [
            "advlist autolink lists link image charmap print preview anchor",
            "searchreplace visualblocks code fullscreen",
            "insertdatetime media table contextmenu paste"
        ],
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
        relative_urls : false,
        remove_script_host : false
    });

    // enable tinyMCE on Terms And Conditions
    tinymce.init({
        mode : "exact",
        elements :"termsAndConditionsHtml",
        plugins: [
            "advlist autolink lists link image charmap print preview anchor",
            "searchreplace visualblocks code fullscreen",
            "insertdatetime media table contextmenu paste"
        ],
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
        relative_urls : false,
        remove_script_host : false      
    });

});
</script>

Now when I try to do

tinyMCE.execCommand('mceInsertContent',false,'<img src="'+imgsrc+'" />');

It inserts the image in the last TinyMCE editor, which is the terms and conditions one, how do I insert that image into the first or second TinyMCE editor?

I have multiple TinyMCE editors on one page and I need to insert HTML in a specific one, currently when I insert HTML, it inserts it into the last one:

My init code:

<script type="text/javascript">
$(document).ready(function() {

    // enable tinyMCE on shortDescription
    tinymce.init({
        mode : "exact",
        elements :"shortDescriptionHtml",
        plugins: [
            "advlist autolink lists link image charmap print preview anchor",
            "searchreplace visualblocks code fullscreen",
            "insertdatetime media table contextmenu paste"
        ],
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
        relative_urls : false,
        remove_script_host : false
    });

    // enable tinyMCE on fullDescription
    tinymce.init({
        mode : "exact",
        elements :"fullDescriptionHtml",
        plugins: [
            "advlist autolink lists link image charmap print preview anchor",
            "searchreplace visualblocks code fullscreen",
            "insertdatetime media table contextmenu paste"
        ],
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
        relative_urls : false,
        remove_script_host : false
    });

    // enable tinyMCE on Terms And Conditions
    tinymce.init({
        mode : "exact",
        elements :"termsAndConditionsHtml",
        plugins: [
            "advlist autolink lists link image charmap print preview anchor",
            "searchreplace visualblocks code fullscreen",
            "insertdatetime media table contextmenu paste"
        ],
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
        relative_urls : false,
        remove_script_host : false      
    });

});
</script>

Now when I try to do

tinyMCE.execCommand('mceInsertContent',false,'<img src="'+imgsrc+'" />');

It inserts the image in the last TinyMCE editor, which is the terms and conditions one, how do I insert that image into the first or second TinyMCE editor?

Share Improve this question asked Sep 11, 2013 at 6:22 Jan Vladimir MostertJan Vladimir Mostert 13k16 gold badges92 silver badges151 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

You should get a certain instance of editor. I guess, something like that: tinymce.get($(el).attr('id'));?

check this out: http://jsfiddle/svCp2/. The line here is:

tinymce.get('c3').setContent('sdf');
tinymce.get("editorID").execCommand(
    'mceInsertContent',
    false,
    '<img src="'+imgsrc+'" />'
);

where "editorID" is your editor ID selector

<div id="editorID">...</div>

Use the activeEditor object:

tinyMCE.activeEditor.execCommand('mceInsertContent',false,'<img src="'+imgsrc+'" />');
Post a comment

comment list (0)

  1. No comments so far