使用 UEditor 编辑内容时,当需要一些列表时,如 <ul> 和 <ol> ,列表中的每一项 UEditor 会自动增加 <p> 标签,就算我切换到 HTML 模式下将 <p> 干掉后,再切换可视化状态就会自动又加上 <p> 标签。
解决办法
1、找到ueditor.all.js(我引用的是ueditor.all.js,有的人用的是ueditor.all.min.js)
2、搜索utils.each(root.getNodesByTagName('li'),function(li) 就可以找到以下代码,删掉或注释
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | //导出时,去掉p标签 //me.getOpt('disablePInList') === true && me.addOutputRule(function(root){ // utils.each(root.getNodesByTagName('li'),function(li){ // var newChildrens = [],index=0; // utils.each(li.children,function(n){ // if(n.tagName == 'p'){ // var tmpNode; // while(tmpNode = n.children.pop()) { // newChildrens.splice(index,0,tmpNode); // tmpNode.parentNode = li; // lastNode = tmpNode; // } // tmpNode = newChildrens[newChildrens.length-1]; // if(!tmpNode || tmpNode.type != 'element' || tmpNode.tagName != 'br'){ // var br = UE.uNode.createElement('br'); // br.parentNode = li; // newChildrens.push(br); // } // index = newChildrens.length; // } // }); // if(newChildrens.length){ // li.children = newChildrens; // } // }); //}); //进入编辑器的li要套p标签 //me.addInputRule(function(root){ // utils.each(root.getNodesByTagName('li'),function(li){ // var tmpP = UE.uNode.createElement('p'); // for(var i= 0,ci;ci=li.children[i];){ // if(ci.type == 'text' || dtd.p[ci.tagName]){ // tmpP.appendChild(ci); // }else{ // if(tmpP.firstChild()){ // li.insertBefore(tmpP,ci); // tmpP = UE.uNode.createElement('p'); // i = i + 2; // }else{ // i++; // } // } // } // if(tmpP.firstChild() && !tmpP.parentNode || !li.firstChild()){ // li.appendChild(tmpP); // } // //trace:3357 // //p不能为空 // if (!tmpP.firstChild()) { // tmpP.innerHTML(browser.ie ? ' ' : '<br/>') // } // //去掉末尾的空白 // var p = li.firstChild(); // var lastChild = p.lastChild(); // if(lastChild && lastChild.type == 'text' && /^\s*$/.test(lastChild.data)){ // p.removeChild(lastChild) // } // }); // if(me.options.autoTransWordToList){ // var orderlisttype = { // 'num1':/^\d+\)/, // 'decimal':/^\d+\./, // 'lower-alpha':/^[a-z]+\)/, // 'upper-alpha':/^[A-Z]+\./, // 'cn':/^[\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+[\u3001]/, // 'cn2':/^\([\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+\)/ // }, // unorderlisttype = { // 'square':'n' // }; // function checkListType(content,container){ // var span = container.firstChild(); // if(span && span.type == 'element' && span.tagName == 'span' && /Wingdings|Symbol/.test(span.getStyle('font-family'))){ // for(var p in unorderlisttype){ // if(unorderlisttype[p] == span.data){ // return p // } // } // return 'disc' // } // for(var p in orderlisttype){ // if(orderlisttype[p].test(content)){ // return p; // } // } // } // utils.each(root.getNodesByTagName('p'),function(node){ // if(node.getAttr('class') != 'MsoListParagraph'){ // return // } // //word粘贴过来的会带有margin要去掉,但这样也可能会误命中一些央视 // node.setStyle('margin',''); // node.setStyle('margin-left',''); // node.setAttr('class',''); // function appendLi(list,p,type){ // if(list.tagName == 'ol'){ // if(browser.ie){ // var first = p.firstChild(); // if(first.type =='element' && first.tagName == 'span' && orderlisttype[type].test(first.innerText())){ // p.removeChild(first); // } // }else{ // p.innerHTML(p.innerHTML().replace(orderlisttype[type],'')); // } // }else{ // p.removeChild(p.firstChild()) // } // var li = UE.uNode.createElement('li'); // li.appendChild(p); // list.appendChild(li); // } // var tmp = node,type,cacheNode = node; // if(node.parentNode.tagName != 'li' && (type = checkListType(node.innerText(),node))){ // var list = UE.uNode.createElement(me.options.insertorderedlist.hasOwnProperty(type) ? 'ol' : 'ul'); // if(customStyle[type]){ // list.setAttr('class','custom_'+type) // }else{ // list.setStyle('list-style-type',type) // } // while(node && node.parentNode.tagName != 'li' && checkListType(node.innerText(),node)){ // tmp = node.nextSibling(); // if(!tmp){ // node.parentNode.insertBefore(list,node) // } // appendLi(list,node,type); // node = tmp; // } // if(!list.parentNode && node && node.parentNode){ // node.parentNode.insertBefore(list,node) // } // } // var span = cacheNode.firstChild(); // if(span && span.type == 'element' && span.tagName == 'span' && /^\s*( )+\s*$/.test(span.innerText())){ // span.parentNode.removeChild(span) // } // }) // } //}); |
原创地址:http://www.luofenming.com/show.aspx?id=ART2020060800001