对于标准textareas,我使用placeholder=""
。我怎样才能扩展tinymce以便它也能以这种方式工作。
与CKEditor相似:http://alfonsoml.blogspot.com.es/2012/04/placeholder-text-in-ckeditor.html
答案 0 :(得分:17)
placeholder plugin对我很有用。 该插件为TinyMCE编辑器带来了HTML5占位符属性功能。
答案 1 :(得分:6)
<html>
<head>
<title>Bhanu Pratap, Tinymce with placeholder... </title>
<script src="http://cdn.tinymce.com/4/tinymce.min.js"></script>
<script type="text/javascript">
tinymce.PluginManager.add('placeholder', function (editor) {
editor.on('init', function () {
var label = new Label;
onBlur();
tinymce.DOM.bind(label.el, 'click', onFocus);
editor.on('focus', onFocus);
editor.on('blur', onBlur);
editor.on('change', onBlur);
editor.on('setContent', onBlur);
function onFocus() { if (!editor.settings.readonly === true) { label.hide(); } editor.execCommand('mceFocus', false); }
function onBlur() { if (editor.getContent() == '') { label.show(); } else { label.hide(); } }
});
var Label = function () {
var placeholder_text = editor.getElement().getAttribute("placeholder") || editor.settings.placeholder;
var placeholder_attrs = editor.settings.placeholder_attrs || { style: { position: 'absolute', top: '2px', left: 0, color: '#aaaaaa', padding: '.25%', margin: '5px', width: '80%', 'font-size': '17px !important;', overflow: 'hidden', 'white-space': 'pre-wrap' } };
var contentAreaContainer = editor.getContentAreaContainer();
tinymce.DOM.setStyle(contentAreaContainer, 'position', 'relative');
this.el = tinymce.DOM.add(contentAreaContainer, "label", placeholder_attrs, placeholder_text);
}
Label.prototype.hide = function () { tinymce.DOM.setStyle(this.el, 'display', 'none'); }
Label.prototype.show = function () { tinymce.DOM.setStyle(this.el, 'display', ''); }
});
tinymce.init({selector: ".EditorControl",plugins: ["placeholder"]});
</script>
</head>
<body>
<textarea class="EditorControl" placeholder="Bhanu Pratap welcomes you, please enter some text here...."></textarea>
</body>
</html>
答案 2 :(得分:2)
使用TinyMCE 3及更低版本,插件工作正常。该插件在TinyMCE 4中不可用,但可以在初始化时添加占位符,然后在焦点上删除它。记住TinyMCE使用iframe。
tinymce.init({
//here all the rest of the options
//xxxxx
//Add the placeholder
setup: function (editor) {
editor.on('init', function(){
if (tinymce.get('Text').getContent() == ''){
tinymce.get('Text').setContent("<p id='#imThePlaceholder'>Your nice text here!</p>");
}
});
//and remove it on focus
editor.on('focus',function(){
$('iframe').contents().find('#imThePlaceholder').remove();
});
})
答案 3 :(得分:1)
TinyMCE 5.2中有一个针对内嵌占位符的新功能。 现在可以为它提供一个自定义占位符的示例:
<script type="text/javascript">
tinymce.init({
selector: "textarea#classic"
});
tinymce.init({
selector: "div#inline",
inline: true,
placeholder: "Type here..."
});
</script>