Ueditor/自定义工具栏
来自站长百科
此条目用于说明Ueditor自定义工具栏
步骤一:创建编辑器
var option = { initialContent: 'hello world',//初始化编辑器的内容 minFrameHeight: 200, iframeCssUrl :'../../themes/default/iframe.css' //给iframe样式的路径 }; //实例化编辑器 var myeditor = new baidu.editor.Editor(option);
步骤二:给自定义的ui注册事件
myeditor.addListener('selectionchange', function (){ var cmdName = ['bold','italic','underline','strikethrough'], fontName = ['fontfamily','fontsize'],i=-1, range = myeditor.selection.getRange();//得到选中区域 //判断是否为封闭标签 if(range.collapsed){ document.getElementById("bold").disabled = true; document.getElementById("italic").disabled = true; document.getElementById("underline").disabled = true; document.getElementById("strikethrough").disabled = true; document.getElementById("fontfamily").disabled = true; document.getElementById("fontsize").disabled = true; document.getElementById("fontColor").disabled = true; }else{ document.getElementById("bold").disabled = false; document.getElementById("italic").disabled = false; document.getElementById("underline").disabled = false; document.getElementById("strikethrough").disabled = false; document.getElementById("fontfamily").disabled = false; document.getElementById("fontsize").disabled = false; document.getElementById("fontColor").disabled = false; } while(i++ < cmdName.length-1){ //分别判断underline和strikethrough的命令 if(cmdName[i] == "underline"){ var val = myeditor.queryCommandValue("underline"); if(val == "underline"){ document.getElementById(cmdName[i]).style.color = "red"; }else{ document.getElementById(cmdName[i]).style.color = ""; } }else if(cmdName[i] == "strikethrough"){ var val = myeditor.queryCommandValue("underline"); if(val == "line-through"){ document.getElementById(cmdName[i]).style.color = "red"; }else{ document.getElementById(cmdName[i]).style.color = ""; } }else{ var state = myeditor.queryCommandState(cmdName[i]); if(state == 1){ document.getElementById(cmdName[i]).style.color = "red"; }else{ document.getElementById(cmdName[i]).style.color = ""; } } } i = -1; while(i++<fontName.length-1){ var fstate = myeditor.queryCommandValue(fontName[i]).toLowerCase(); var fselect = document.getElementById(fontName[i]); for(var j= 0;j<fselect.options.length;j++){ if(fselect.options[j].value.toLowerCase().indexOf(fstate.split(",")[0])!=-1){ fselect.options[j].selected = "true"; } } } var fcolor = myeditor.queryCommandValue("forecolor"); document.getElementById("fontColor").style.color = fcolor; });
参考来源[ ]
Ueditor使用手册导航 | ||||||
---|---|---|---|---|---|---|
|