KindEditor富文本框在dialog下的创建与使用
jquery框架下使用KindEditor
在主流的前端框架(如:bootstrap)下在dialog页使用富文本编辑器并不会出现无法编辑的现象,而jquery则存在某种限制,我尝试很多方法然后使用其中的一种,大家有什么其他想法也可以分享讨论。
创建editor
html代码
<!-- demo内容弹框 -->
<div id="demo-dialog" style="display: none">
<!-- 编辑区域 -->
<div class="col-md-12" id="demo_content" style="margin-top: 10px">
<div style="padding:4px 0px;">
<textarea name="demo_contents" id="demo_contents" style="width:100%;height:300px;"></textarea>
</div>
</div>
</div>
js代码
$("#demo-link").click(function () {
//显示dialog
$("#demo-dialog").dialog({
});
//创建editor
demo_editor = KindEditor.create('textarea[name="demo_contents"]', {
filterMode: true,
items: items,
allowPreviewEmoticons: true,
afterCreate: function () {
this.sync();
}
});
});
如果是点击事件触发dialog, 必须等dialog页加载完毕,再进行创建editor,否则就会导致无法编辑。
editor内容改变事件
//放在页面加载之前
var first_service_content = null;
//保存按钮禁启用(editor创建以后)
$("#demo-dialog").parent().parent().parent().find('input[name="保存"]').attr('disabled',"true");
$("#demo-dialog").parent().parent().parent().find('input[name="保存"]').css("background-color", "silver");
//editor内容改变事件(editor创建以后)
demo_editor.edit.afterChange(function (fn) {
if(demo_editor != null) {
if(demo_editor.html() == first_demo_content) {
$("#demo-dialog").parent().parent().parent().find('input[name="保存"]').attr('disabled',"true");
$("#demo-dialog").parent().parent().parent().find('input[name="保存"]').css("background-color", "silver");
} else {
$("#demo-dialog").parent().parent().parent().find('input[name="保存"]').removeAttr("disabled");
$("#demo-dialog").parent().parent().parent().find('input[name="保存"]').css("background-color", "#75B9E6");
}
}
});
//加载demo初始化内容
function loadDemoContent() {
//从后台获取demo初始化内容
}
这里利用editor内容改变事件控制dialog保存按钮的禁启用以及颜色变换,其他一些操作同样可以这样控制,需记住editor的改变事件是在editor创建以后,而且必须在点击事件里面。