所以我对tinymce4 api有一个小问题,iv创建了一个自定义格式,我想用一个按钮触发它。然而,发生的情况是,当单击按钮时,样式将应用于按钮而不是实际的可内容编辑字段。
tinymce.init({
selector: '#editable',
inline: true,
menubar: false,
toolbar:false,
statusbar: false,
});
setTimeout(function(){
tinymce.activeEditor.formatter.register('mycustomformat', {
inline : 'span',
styles: {color: 'red'}
});
},200);
$('.js-toggleformat').on('click', function(e) {
tinymce.activeEditor.formatter.apply('mycustomformat');
})
和 html:
<button class="js-toggleformat">Toggle</button>
<div id="editable" contenteditable="true"></div>
最佳答案
看一个例子。 tinymce 插件“textcolor”使用函数“applyFormat”来应用颜色。它看起来像这样:
function applyFormat(format, value) {
editor.focus();
editor.formatter.apply(format, {value: value});
editor.nodeChanged();
}
基于此,这应该适用于您的情况:
tinymce.activeEditor.focus();
tinymce.activeEditor.formatter.apply('mycustomformat');
tinymce.activeEditor.nodeChanged();
关于javascript - Tinymce 切换格式,格式化按钮而不是编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18101288/