我有添加按鈕和刪除按鈕,動態添加和刪除tinymce textarea。它的工作原理。我的代碼的問題是每當我刪除tinymce textarea並將其添加回來。 tinymce不會使用textarea進行初始化。如何動態添加和刪除tinymce
我的代碼的鏈接是jsfiddle
我的HTML代碼
<ol type="A" id="list">
<li id="element1">
<input type="radio" name="optionanswer" id="answer1" value="1" >
<textarea class="ans" name="option1" id="option1" rows="2" cols="3">
</textarea>
</li>
</ol>
<button class="btn btn-primary" type="submit" id="addalt"><span class="glyphicon glyphicon-plus"></span> Add Alternative</button>
我的javascript代碼
$(document).ready(function(){
// function to run tinymce
function tinym(){
tinymce.init({
selector: 'textarea',
menubar:false,
statusbar: false,
toolbar: "charmap",
plugins: [
' charmap'
],
content_css: [
'//fonts.googleapis.com/css?family=Lato:300,300i,400,400i',
'//www.tinymce.com/css/codepen.min.css'
]
});
}
tinym();
$(document).on("click", "#addalt" , function() {
event.preventDefault();
var ul = document.getElementById("list");
var li = document.createElement("li");
var children = ul.children.length + 1
//li.setAttribute("id", "element"+children)
//console.log(children);
var idname='answer'+children;
var textid='option'+children;
$("ol").append("<li> <input type='radio'name='optionanswer' id=" + idname + " /> <textarea rows='2' cols='3' name=" + textid + " id=" + textid + "> </textarea> <a href='javascript:void(0);' class='remove'>×</a></li>");
tinym();
});
$(document).on("click", "a.remove" , function() {
$(this).parent().remove();
});
});
可能的重複[如何刪除tinyMCE,然後重新添加它?](http://stackoverflow.com/questions/4651676/how-do-i-remove-tinymce-and-then-re-add - 它) – Val
請參閱實施[這裏](http://phpflow.com/demo/add-remove-tinymce-4-demo/) 請參閱 – crowchirp