基本思路:当用户要使用编辑器输入文本时,再对输入框进行实例化,当编辑器失去焦点时,根据输入内容是否为空,进行应对判断是否销毁ueditor对象
代码:
<!DOCTYPE html>
<html>
<head>
<title>ueditor</title>
<meta charset="utf-8">
<style>
#editor {
width: 600px;
height:400px;
margin:auto;
}
/*** placeholder文本样式 ***/
#editor::-ms-input-placeholder {
font-size: 1.5em;
text-align: center;
}
#editor::-webkit-input-placeholder{
font-size: 1.5em;
text-align: center;
}
</style>
</head>
<body>
<div>
<textarea id="editor" placeholder="好好学习天天上上" ></textarea>
</div>
<script type="text/javascript" charset="utf-8" src="ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="ueditor.all.min.js"> </script>
<script type="text/javascript" charset="utf-8" src="lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript">
window.οnlοad=function(){
var ueObj = null; //编辑器实例
var ueDom = document.getElememtById('editor');
//添加事件监听
ueDom.addEventListener("focus", function(){
if(ueObj) return ; //如果编辑器已经实例化,直接返回
ueObj = UE.getEditor('editor', {focus:true}); //取得ueditor实例
ueObj.addListener("blur", function(){ //输入框失去焦点时,如果内容为空,销毁ueditor
if(ueObj.getContent().length == 0) {
UE.getEditor('editor').destroy();
ueObj= null;
}
})
})
}
</script>
</body>
</html>