我有一个文本区域,我在该文本区域上使用 tinyMCE。
我实际上做的是,当页面打开时,我用一些文本填充文本区域,然后我初始化 tinyMCE。
问题是当我尝试在 tinyMCE 初始化后更改文本区域的值时,没有任何反应。
这是一个例子。
创建文本区域:
<textarea style="width: 95%;" name="title" id="title"></textarea>
填充文本区域:
$('#title').html("someText");
初始化tinyMCE
tinyMCE.init({ // General options mode : "specific_textareas", theme : "advanced", width: "100%", plugins : "pagebreak,paste,fullscreen,visualchars", // Theme options theme_advanced_buttons1 : "code,|,bold,italic,underline,|,sub,sup,|,charmap,|,fullscreen,|,bullist,numlist,|,pasteword", theme_advanced_buttons2 :"", theme_advanced_buttons3 :"", theme_advanced_buttons4 :"", theme_advanced_toolbar_location : "top", theme_advanced_toolbar_align : "left", theme_advanced_statusbar_location : "bottom", valid_elements : "i,sub,sup", invalid_elements : "p, script", editor_deselector : "mceOthers" });
我想更改 TextView 的内容(但它不起作用)
我尝试使用与之前相同的方式初始化 tinyMCE
$('#title').html("someModifiedText"); // does not work
我也尝试过删除 tinyMCE:
if(tinyMCE.getInstanceById('title'))
removeTinyMCE("title");
与
function removeTinyMCE (dialogName) {
tinyMCE.execCommand('mceFocus', false, dialogName);
tinyMCE.execCommand('mceRemoveControl', false, dialogName);
然后重用它们:
$('#title').html("someModifiedText"); // does not work
我没有想法......非常感谢你的帮助......
最佳答案
这里的问题是,如果您在文本区域中输入文本或 html,您将看不到任何内容。 当 tinymce 初始化时,你的文本区域被隐藏。您随后看到的是一个内容可编辑的 iframe,用于编辑和设置内容样式。有几个事件会导致 tinymce 将其内容写入编辑器的 html 源元素(在您的情况下是文本区域)。
如果你想设置编辑器的内容(这是可见的)你需要调用类似的东西
tinymce.get('title').setContent('<p>This is my new content!</p>');
你也可以使用下面的方式直接访问dom元素
tinymce.get('title').getBody().innerHTML = '<p>This is my new content!</p>';
或使用 jQuery
$(tinymce.get('title').getBody()).html('<p>This is my new content!</p>');
关于javascript - 在 TinyMCE 初始化后使用 javascript 设置 textarea 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11793368/