django后台模板admin在使用百度富文本编辑ueditor的bug解决
记录一下,花了点时间想明白这个问题。
首先我要做的是:兼职的项目,django的后台(admin那个)有一个表是新闻,为了项目的运营人员的好操作,我要把这个模块改成富文本编辑。
然后为了改的最少,我的偷懒方法是:后台模板自动输出的新闻内容有一个div,我们叫做div1好了,然后富文本编辑有一个编辑框,我们叫做div2好了。
然后就是div1里面的内容先读进变量temp,把temp用uediter的方法setContent写入div2,然后就可以富文本表示。
然后就是bug了。
显示的是上述的东西,并没有是 红色的wang。
后面那个div不知道是什么鬼,用ueditor的getContent取出就会自带的,不影响就不关了。
解决:
用console输出变量发现问题所在:因为是从html内容中取出的,所以<>都被转义了。
<p><span style="color:#FF0000;">wang</span></p>
所以只要把变量中的<和>再转义一下就可以了。
最后结果:
部分代码:
$("<div id='myEditor' style='width:800px;height:240px;float:left''></div><input id='test' value='save' type='button' />").insertAfter($(".aligned>.field-content label")); $(".aligned>.field-content").css("height","800px"); $(".aligned>.field-content label").css("width","400px"); var temp = $(".aligned>.field-content #id_content").html(); var temp3 = temp.replace(/</g,"<").replace(/>/g,">"); var ue = new baidu.editor.ui.Editor({ }); ue.render("myEditor"); ue.setContent(temp3); $("#test").click(function(){ var html = ue.getContent(); $(".aligned>.field-content #id_content").html(html); });
然后django模板的修改以及ueidtor的使用有什么问题可以一起交流,共同进步。