百度UEditor编辑器API文档
UE.Editor
依赖
editor.js, UE.Utils , UE.EventBase , UE.browser , core/dom/dtd.js, UE.dom.domUtils , UE.dom.Range , core/dom/Selection.js, plugins/serialize.js
编辑器主类,包含编辑器提供的大部分公用接口
Editor
UEditor编辑器类
创建一个跟编辑器实例
- container 编辑器容器对象
- iframe 编辑区域所在的iframe对象
- window 编辑区域所在的window
- document 编辑区域所在的document对象
- body 编辑区域所在的body对象
- selection 编辑区域的选区对象
ready
- editor.ready(fn) fn是当编辑器渲染好后执行的function
当编辑器ready后执行传入的fn,如果编辑器已经完成ready,就马上执行fn,fn的中的this是编辑器实例。
大部分的实例接口都需要放在该方法内部执行,否则在IE下可能会报错。
var editor = new UE.ui.Editor(); editor.render("myEditor"); editor.ready(function(){ editor.setContent("欢迎使用UEditor!"); })
destroy
- editor.destroy();
销毁编辑器实例对象
render
- editor.render(containerId); //可以指定一个容器ID
- editor.render(containerDom); //也可以直接指定容器对象
渲染编辑器的DOM到指定容器,必须且只能调用一次
sync
- editor.sync(); //从编辑器的容器向上查找,如果找到就同步数据
- editor.sync(formID); //formID制定一个要同步数据的form的id,编辑器的数据会同步到你指定form下
同步编辑器的数据,为提交数据做准备,主要用于你是手动提交的情况
后台取得数据得键值使用你容器上得name属性,如果没有就使用参数传入的textarea
editor.sync(); form.sumbit(); //form变量已经指向了form元素
setHeight
- editor.setHeight(number); //纯数值,不带单位
设置编辑器高度
getContent
- editor.getContent() ⇒ String //若编辑器中只包含字符"<p><br /></p/>"会返回空。
- editor.getContent(fn) ⇒ String
获取编辑器内容
getContent默认是会现调用hasContents来判断编辑器是否为空,如果是,就直接返回空字符串 你也可以传入一个fn来接替hasContents的工作,定制判断的规则 editor.getContent(function(){ return false //编辑器没有内容 ,getContent直接返回空 })
getAllHtml
- editor.getAllHtml() ⇒ String
取得完整的html代码,可以直接显示成完整的html文档
getPlainTxt
- editor.getPlainTxt() ⇒ String
得到编辑器的纯文本内容,但会保留段落格式
getContentTxt
- editor.getContentTxt() ⇒ String
获取编辑器中的纯文本内容,没有段落格式
setContent
- editor.setContent(html)
将html设置到编辑器中, 如果是用于初始化时给编辑器赋初值,则必须放在ready方法内部执行
var editor = new UE.ui.Editor() editor.ready(function(){ //需要ready后执行,否则可能报错 editor.setContent("欢迎使用UEditor!"); })
focus
- editor.focus([toEnd]) //默认focus到编辑器头部,toEnd为true时focus到内容尾部
让编辑器获得焦点,toEnd确定focus位置
execCommand
- editor.execCommand(cmdName) ⇒ {*}
执行编辑命令cmdName,完成富文本编辑效果
queryCommandState
- editor.queryCommandState(cmdName) ⇒ (-1|0|1)
根据传入的command命令,查选编辑器当前的选区,返回命令的状态
- -1 当前命令不可用
- 0 当前命令可用
- 1 当前命令已经执行过了
queryCommandValue
- editor.queryCommandValue(cmdName) ⇒ {*}
根据传入的command命令,查选编辑器当前的选区,根据命令返回相关的值
hasContents
- editor.hasContents() ⇒ (true|false)
- editor.hasContents(tags) ⇒ (true|false) //若文档中包含tags数组里对应的tag,直接返回true
检查编辑区域中是否有内容,若包含tags中的节点类型,直接返回true
默认有文本内容,或者有以下节点都不认为是空
{table:1,ul:1,ol:1,dl:1,iframe:1,area:1,base:1,col:1,hr:1,img:1,embed:1,input:1,link:1,meta:1,param:1}
editor.hasContents(['span']) //如果编辑器里有这些,不认为是空
reset
- editor.reset()
重置编辑器,可用来做多个tab使用同一个编辑器实例
- 清空编辑器内容
- 清空回退列表
enable
- editor.enable()
设置当前编辑区域可以编辑
disable
- editor.disable()
- editor.disable(except) //例外的命令,也即即使设置了disable,此处配置的命令仍然可以执行
设置当前编辑区域不可编辑,except中的命令除外
//禁用工具栏中除加粗和插入图片之外的所有功能 editor.disable(['bold','insertimage']);//可以是单一的String,也可以是Array
show
- editor.show()
显示编辑器
hide
- editor.hide()
隐藏编辑器
getLang
- editor.getLang(path) ⇒ (JSON|String) 路径根据的是lang目录下的语言文件的路径结构
根据制定的路径,获取对应的语言资源
editor.getLang('contextMenu.delete') //如果当前是中文,那返回是的是删除
getDialog
- editor.getDialog(dialogName) ⇒ Object
得到dialog实例对象
var dialog = editor.getDialog("insertimage"); dialog.open(); //打开dialog dialog.close(); //关闭dialog
加支付宝好友偷能量挖...