在使用 Wordpress 的 tinymce 编辑器时,我注意到他们能够让用户在富文本模式下进行编辑,在这种模式下他们可以看到所有格式,也可以切换回纯文本模式,在这种模式下用户可以进行编辑html/script/css 或任何其他代码。
我是tinymce的新手,不太了解它。我能够使用默认的 javascript 让tinymce 工作:
tinymce.init({ selector:'#id_content' });
在谷歌搜索时,我发现的第一件事是:
http://archive.tinymce.com/tryit/3_x/toggle_editor.php
这准确地显示了我想要的内容,但没有解释如何在任何地方做到这一点。该示例中有太多选项和插件,我不知道发生了什么。查看源代码,仅源代码甚至不具备功能。有一个“ fiddle 这个例子”( http://fiddle.tinymce.com/paaaab ),但即使 fiddle 也不起作用。
经过更多的谷歌搜索,我找到了这个问题/答案:Plain and Rich text editor options in TinyMce editor
这个问题似乎问了和我问的同样的事情(这个人在发布问题前 6 天就问了)但是,接受的答案现在已经过时了,没有多大意义。我尝试了第二个答案,确切的代码没有任何改变,但它根本不起作用。
如果有人可以解决这个问题,或者告诉我为什么我的努力不起作用,我会欣喜若狂!
最佳答案
您链接到的 Fiddle 不起作用,因为它正在调用最新的 TinyMCE javascript 库。我将其更改为使用 TinyMCE v.3.5.11:
http://fiddle.tinymce.com/Cnfaab
切换格式现在可以使用。
<a class="btn" href="javascript:;" onclick="tinymce.execCommand('mceToggleEditor',false,'content');"><span>Toggle TinyMCE</span></a>
在 TinyMCE4 中:
不确定这是否是正确的方法,但我相信这可以实现您想要做的事情: http://codepen.io/anon/pen/ZQXLBo
JS:
tinymce.init({
selector: 'textarea',
height: 500,
toolbar: 'mybutton',
menubar: false,
setup: function(editor) {
editor.addButton('mybutton', {
text: 'My button',
icon: false,
onclick: function(e) {
var $ = tinymce.dom.DomQuery;
var myTextarea = $('textarea');
var myIframe = $(editor.iframeElement);
myTextarea.value = editor.getContent({
source_view: true
});
myIframe.toggleClass("hidden");
myTextarea.toggleClass("visible");
if ($('iframe.hidden').length > 0) {
myTextarea.prependTo(".mce-edit-area");
} else {
myIframe.value = myTextarea.value;
myTextarea.appendTo('body');
}
}
});
},
content_css: [
'//fast.fonts.net/cssapi/e6dc9b99-64fe-4292-ad98-6974f93cd2a2.css',
'//www.tinymce.com/css/codepen.min.css'
]
});
HTML:
<textarea>
<p style="text-align: center; font-size: 15px;"><img title="TinyMCE Logo" src="//www.tinymce.com/images/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="70171c0900185d04191e091d13153042085e001e17" rel="noreferrer noopener nofollow">[email protected]</a>" alt="TinyMCE Logo" width="110" height="97" />
</p>
<h1 style="text-align: center;">Welcome to the TinyMCE editor demo!</h1>
<p>Select a menu item from the listbox above and it will insert contents into the editor at the caret position.</p>
<h2>Got questions or need help?</h2>
<ul>
<li>Our <a href="http://www.tinymce.com/docs/">documentation</a> is a great resource for learning how to configure TinyMCE.</li>
<li>Have a specific question? Visit the <a href="http://community.tinymce.com/forum/">Community Forum</a>.</li>
<li>We also offer enterprise grade support as part of <a href="http://www.tinymce.com/pricing">TinyMCE Enterprise</a>.</li>
</ul>
<h2>Found a bug?</h2>
<p>If you think you have found a bug please create an issue on the <a href="https://github.com/tinymce/tinymce/issues">GitHub repo</a> to report it to the developers.</p>
<h2>Finally ...</h2>
<p>Don't forget to check out our other product <a href="http://www.plupload.com" target="_blank">Plupload</a>, your ultimate upload solution featuring HTML5 upload support.</p>
<p>Thanks for supporting TinyMCE! We hope it helps you and your users create great content.
<br>All the best from the TinyMCE team.</p>
</textarea>
CSS:
textarea {
height: 500px !important;
width: 100% !important;
position: absolute;
z-index: 100;
}
.hidden {
visibility: hidden !important;
display: none !important;
}
.visible {
visibility: visible !important;
display: block !important;
}
关于javascript - 使用tinymce 在 HTML 预览和纯文本之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34801808/