2016-01-21 219 views
3

我在前端設置了編輯器並出現問題,在這種情況下如何初始化tinymce初始化Tinymce的問題

我的代碼:

<head> 
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script> 
</head> 
<body> 
    <a class=pageedit>Редактировать</a> 
<div id="textwidget" class="textwidget"> 
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati esse enim facilis quam magnam nihil excepturi ipsa, maxime ducimus sapiente, repudiandae facere mollitia, velit quia dolore doloribus molestiae odit fuga? 
</div> 
</body> 

/*JQUERY*/ 
    $(document).ready(function() { 

     $('a.pageedit').on('click', function() { 

      $('#textwidget').wrap('<form class="tinymce"><textarea class="tiny" name="page"></textarea></form>'); 

      $(this).unbind('click'); 
      setTimeout(function() { 
      Tinit(); 
      },100); 
     }); 

    }); 

function Tinit() { 
     tinymce.init({ 
     selector:'textarea.tiny', 
     height: 600, 
     plugins: [ 
      'advlist autolink lists link image charmap print preview anchor', 
      'searchreplace visualblocks code fullscreen', 
      'insertdatetime media table contextmenu paste code' 
     ], 
     toolbar: 'undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image', 
     }); 

     $('form.tinymce').submit(function() { 
     return false; 
     }); 

     $('form.tinymce').on('submit', function() { 
     var page = $(this).serialize(); 
     $.ajax({ 
      type:'POST', 
      url: 'publish.php', 
      data: page, 
      success: function(data) { 
      alert(data); 
      document.location.href = ''; 
      }, 
      error: function() { 
      alert('error'); 
      } 
     }); 
     }); 

} 

最終,編輯器被加載,但裏面空空如也。我該如何解決它?

轉載於此:http://codepen.io/anon/pen/PZEEwG

回答

2

這很容易做到。在初始化tinymce之前,確保tinymce源元素包含所需的內容。 另一種選擇是在初始化後設置tinymce內容。

tinymce.init({ 
    selector:'textarea.tiny', 
    height: 600, 
    plugins: [ 
     'advlist autolink lists link image charmap print preview anchor', 
     'searchreplace visualblocks code fullscreen', 
     'insertdatetime media table contextmenu paste code' 
    ], 
    toolbar: 'undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image', 
    setup : function(ed) 
    { 

     ed.on('init', function(evt) 
     { 
     ed.setContent($('#textwidget').html()); 
     }); 
    } 
    }); 

.... 
} 

更新:在提交做到:

$('form.tinymce').on('submit', function() { 
    // Calls the save method on all editor instances in the collection. This can be useful when a form is to be submitted. 
    tinyMCE.triggerSave(); 

    .... 
}); 
+0

現在在編輯器中的HTML代碼,但在textarea的發送形式向服務器時發送的形式執行主編之前空 –

+0

。 triggerSave();這將更新tinymce源元素與編輯內容 – Thariama

+0

sry,更正了我的答案 – Thariama