百度编辑器UEditor的内容改变事件,自动保存功能实现方法
微wx笑
2022-06-15【网页网站】
128
2
0关键字:
百度编辑器UEditor的内容改变事件
目录
百度编辑器UEditor的内容改变事件
监听内容改变事件
1 2 | UE.getEditor( 'content' ).addListener( 'contentChange' , function (editor){ //相关操作}); |
注:其中content代表你的富文本编辑器的id值
回调函数中的参数editor值是事件名称contentchange,基本没有用处。
自动保存功能实现方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | var currentNewsId = 0; //记录当前信息的id var currentContent = "" ; //记录当前信息的内容 var autoSaveTimer = null ; //自动保存计时器 ueInstanse.addListener( 'contentChange' , function (evtname){ if (autoSaveTimer){ window.clearTimeout(autoSaveTimer); //如果存在则清除,也就不没到1秒的间隔,内容又发生改变的情况 } autoSaveTimer = window.setTimeout( function () { autoSaveContent(); //1秒后执行自动保存方法 }, 1000); }); function autoSaveContent(){ if (!newstext.isReady){ return ; } var form = { action: "save" , id: currentNewsId, title:$( "#title" ).val(), content: newstext.getContent() }; if (form.content == currentContent){ return ; } $.ajax({ type: 'POST' , url: "autosave.php" , data: form, dataType: 'html' , success: function (data){ if (data == '' ){ console.log( '登录过期' ); return ; } var rlt = JSON.parse(data); if (rlt.code == 0 && 'id' in rlt){ currentContent = form.content; console.log( "auto save success." ); } else { alert(rlt.code); } } }); } |
本文由 微wx笑 创作,采用 署名-非商业性使用-相同方式共享 4.0 许可协议,转载请附上原文出处链接及本声明。
原文链接:https://www.ivu4e.cn/blog/web/2022-06-15/1242.html