KindEditor富文本框无法编辑或者内容改变无法触发事件

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创建以后,而且必须在点击事件里面。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 首先,在 `application\extra` 目录下新建一个 `config.php` 文件,添加以下配置: ```php <?php return [ 'upload_path' => '/uploads/', //上传文件保存的路径 'upload_exts' => 'gif,jpg,jpeg,png,bmp', //允许上传的文件类型 ]; ``` 2. 在 `application\index\controller` 目录下新建一个 `Upload.php` 控制器,添加以下代码: ```php <?php namespace app\index\controller; use think\Controller; use think\Request; class Upload extends Controller { public function uploadImage() { $file = request()->file('imgFile'); $info = $file->validate(['ext' => config('upload_exts')])->move(ROOT_PATH . 'public' . config('upload_path')); if ($info) { $image_url = config('upload_path') . $info->getSaveName(); return json(['error' => 0, 'url' => $image_url]); } else { return json(['error' => 1, 'message' => $file->getError()]); } } } ``` 3. 在 `application\index\view` 目录下新建一个 `upload_image.html` 文件,添加以下代码: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>上传图片</title> </head> <body> <form action="<?php echo url('upload/uploadImage'); ?>" enctype="multipart/form-data" method="post"> <input type="file" name="imgFile"> <input type="submit" value="上传"> </form> </body> </html> ``` 4. 修改 `kindeditor` 配置文件 `public\static\kindeditor\config.js`,添加以下代码: ```javascript //上传图片配置 imageUploadJson = '<?php echo url("upload/uploadImage"); ?>'; ``` 5. 在 `public\static\kindeditor\plugins` 目录下新建一个 `image.php` 文件,添加以下代码: ```php <?php //获取上传文件保存的路径 $save_path = $_SERVER['DOCUMENT_ROOT'] . $_POST['save_path']; //获取要上传的文件 $file = $_FILES['imgFile']; //上传文件 if (move_uploaded_file($file['tmp_name'], $save_path . $file['name'])) { $image_url = $_POST['base_url'] . $_POST['save_path'] . $file['name']; //返回上传结果 echo json_encode(['error' => 0, 'url' => $image_url]); } else { echo json_encode(['error' => 1, 'message' => '上传失败']); } ``` 6. 修改 `kindeditor` 的 `php` 上传文件处理程序 `public\static\kindeditor\php\upload_json.php`,添加以下代码: ```php //获取要保存的文件路径和文件名 $save_path = $_SERVER['DOCUMENT_ROOT'] . $_POST['save_path']; $file_name = uniqid() . strrchr($_FILES['imgFile']['name'], '.'); //上传文件 if (move_uploaded_file($_FILES['imgFile']['tmp_name'], $save_path . $file_name)) { $image_url = $_POST['base_url'] . $_POST['save_path'] . $file_name; //返回上传结果 echo json_encode(['error' => 0, 'url' => $image_url]); } else { echo json_encode(['error' => 1, 'message' => '上传失败']); } ``` 7. 在浏览器中访问 `http://localhost/index/upload_image.html`,上传一张图片测试。如果上传成功,会返回图片的 URL 地址。在 `kindeditor` 中也可以正常上传图片了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值