单击按钮将文本区域更改为 tinyMCE 编辑器

Change textarea to tinyMCE Editor on button click

我想通过单击按钮将普通 textarea 编辑器转换为 tinyMCE 编辑器。场景是我的页面上有两个按钮,两个 textarea 从一个按钮单击一个 jquery 函数将被调用,从另一个按钮将调用第二个 jquery 按钮,函数是调用正确,但问题是单击特定按钮时,正常 textarea 未转换为 tinyMCEEditor。请帮助解决下面是我的工作代码,还有一件事之后会有保存按钮,所以在保存按钮上单击 tinyMCE 编辑器可以很容易地更改为正常 textarea

index.php:

<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="http://tinymce.cachefly.net/4.2/tinymce.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function()
{
   function refreshdiv()
   {
    $.ajax({
    type:"POST",
    url: 'load_tinymce.php',
    cache:false,
    success:function(html){

    var mainDiv = $('.attach_tiny_editor');
    mainDiv.empty();  
    $(mainDiv).prepend(html);

    },
    beforeSend: function(){

    },
    complete: function(){


    }  
    });
    }

    refreshdiv();



});

$(document).ready(function(){

$(document.body).on('click', '#refresh_1' ,function(e){
      refreshdiv();

      tinyMCE.init({mode : "none"});
      tinyMCE.execCommand('mceAddControl', false, 'txt12');
});

$(document.body).on('click', '#refresh_2' ,function(e){
      refreshdiv();
      tinyMCE.execCommand('mceAddControl', false, 'txt13');
});

});

</script>
<div class="attach_tiny_editor">

</div>
<input type="button" id="refresh_1" value="refresh"/>

<input type="button" id="refresh_2" value="refresh"/>
</body>
</html>

load_tinymce.php

<div>
<textarea id="txt12">   </textarea>
<textarea id="txt13">   </textarea>
</div>

您需要将 tinymce 初始化放入 ajax 成功处理程序函数中才能执行。否则文本区域在需要时不存在。

EDIT: 这只会初始化一个编辑器!

$(document).ready(function()
{
   function refreshdiv()
   {
    $.ajax({
    type:"POST",
    url: 'load_tinymce.php',
    cache:false,
    success:function(html){

    var mainDiv = $('.attach_tiny_editor');
    mainDiv.empty();  
    $(mainDiv).prepend(html);

    tinymce.init({selector:'textarea#' + window.ed_id});

    },
    beforeSend: function(){

    },
    complete: function(){

    }  
    });
    }

    refreshdiv();
});

$(document.body).on('click', '#refresh_1' ,function(e){
      window.ed_id = 'txt12';
      refreshdiv();

      tinyMCE.init({mode : "none"});
      tinyMCE.execCommand('mceAddControl', false, 'txt12');
});

$(document.body).on('click', '#refresh_2' ,function(e){
      window.ed_id = 'txt13';          
      refreshdiv('txt13');
      tinyMCE.execCommand('mceAddControl', false, 'txt13');
});