Javascript 使用 jQuery 在 TinyMCE 编辑器中设置值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8576078/
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-08-24 06:33:45  来源:igfitidea点击:

Set Value Inside a TinyMCE Editor using jQuery

javascriptjquerytinymcetextarea

提问by Mike

Hi I need to set predefined content inside the tinyMCE Editor. Below is my html and jquery.

嗨,我需要在 tinyMCE 编辑器中设置预定义的内容。下面是我的 html 和 jquery。

<script type="text/javascript">
    tinyMCE.init( {
        mode : "exact" ,
        elements : "country"
    });
</script>
<script type="text/javascript">
    $(function() {
        $("#lang").change(function() {
            var s = $(this).val(); alert(s);
            $("#country").val(s);
        })
    })
</script>


<select id="lang">
        <option value="">Please Select country</option>
        <option value="us">US</option>
        <option value="es">SPAIN</option>
        <option value="jp">JAPAN</option>
    </select><br /><br />
    <textarea id="country" cols="10" rows="5"></textarea>

The script works for a normal textarea but not for tinyMCE. Is there anything I am doing wrong in this.

该脚本适用于普通文本区域,但不适用于 tinyMCE。我在这方面做错了什么。

Thanks

谢谢

回答by karim79

I think you can do:

我认为你可以这样做:

$(function() {
    $("#lang").change(function() {
        var s = $(this).val(); 
        alert(s);
        tinyMCE.activeEditor.setContent(s);
    });
});

回答by Juanito

For me only that's code works :

对我来说只有代码有效:

tinyMCE.get('my_textarea_id').setContent(my_value_to_set);

Maybe this is the code from the new version of tinyMCE ! (Tiny MCE Api 3)

也许这是新版本 tinyMCE 的代码!(微型 MCE Api 3)

回答by AtanuCSE

Simply this works for me

只是这对我有用

$("#description").val(content);

$("#description").val(content);

回答by shashikant pandit

You can also try this:
Without Replace whole content inside tinymce, set cursor where you want add value inside tinymce

你也可以试试这个:
不用在tinymce中替换整个内容,在tinymce中设置你想要添加值的光标

$(document).on('change','#lang', function() {
     var Getname = $(this).val();
     if (Getname != '') {
        //tinyMCE.activeEditor.setContent(s);   // This is for replace all content
        tinyMCE.activeEditor.execCommand('mceInsertContent',false,Getname); // Append new value where your Cursor
        //console.log(Getname)
    }
 });

I am using this code the new version of TinyMCE 5

我在TinyMCE 5的新版本中使用此代码