我想在CKEditor中制作插件。当我部分选择文本并单击插件按钮时,它会将自定义标记放在所选文本上。提及代码会相应地工作。但是未选中文本的属性会发生变化
var selection = editor.getSelection();
var range = selection.getRanges();
var cdlToc=editor.document.createElement('cdl:toc');
var pageNumber = parseInt($('#hiddenPageNumber').val());
cdlToc.setAttribute("pageNumber", pageNumber);
cdlToc.setStyle( 'color', 'blue' );
var extractedContent = range[0].extractContents(false);
var test=editor.document.createElement('test');
test.append(extractedContent)
var childCount=test.getChildCount();
for(var i=0; i < childCount; i++){
var child=test.getChild(i);
if(child.getName()=='p')
cdlToc.appendHtml(child.getHtml());
else
cdlToc.appendHtml(child.getOuterHtml());
}
var paragraph=editor.document.createElement('p');
paragraph.appendHtml(cdlToc.getOuterHtml());
paragraph.unselectable();
selection.selectRanges(range[0]);
editor.insertHtml(paragraph.getOuterHtml());
在插入标签之前源代码看起来
<p><cdl:metadata fname="OFAZVP+HelveticaNeueLTStd-LtCn" fsize="10.0" pagenumber="1" x="499.954" y="37.493">Research</cdl:metadata></p>
<p><cdl:metadata fname="OFAZVP+HelveticaNeueLTStd-LtCnO" fsize="8.0" pagenumber="1" x="56.693" y="805.104">Bull World Health Organ</cdl:metadata><cdl:metadata fname="OFAZVP+HelveticaNeueLTStd-LtCn" fsize="8.0" pagenumber="1" x="124.044" y="805.104"> 2010;88:887–896G </cdl:metadata><cdl:metadata fname="OFAZVP+HelveticaNeueLTStd-HvEx" fsize="12.0" pagenumber="1" x="186.099" y="805.104">|</cdl:metadata><cdl:metadata fname="OFAZVP+HelveticaNeueLTStd-LtCn" fsize="8.0" pagenumber="1" x="188.763" y="805.104"> doi:10.2471/BLT.09.071852</cdl:metadata></p>
<p><cdl:metadata fname="PNKFJN+HelveticaNeueLTStd-BdCn" fsize="13.0" pagenumber="1" x="56.693" y="367.697">Introduction</cdl:metadata></p>
插入标签后
<p><cdl:metadata fname="PNKFJN+HelveticaNeueLTStd-BdCn" fsize="13.0" pagenumber="1" x="56.693" y="367.697">Rese</cdl:metadata></p>
<p style="-moz-user-select: none;"><cdl:toc pagenumber="1" style="color: blue;"><cdl:metadata fname="OFAZVP+HelveticaNeueLTStd-LtCn" fsize="10.0" pagenumber="1" x="499.954" y="37.493">arch</cdl:metadata><br />
<cdl:metadata fname="OFAZVP+HelveticaNeueLTStd-LtCnO" fsize="8.0" pagenumber="1" x="56.693" y="805.104">Bull World Health Organ</cdl:metadata><cdl:metadata fname="OFAZVP+HelveticaNeueLTStd-LtCn" fsize="8.0" pagenumber="1" x="124.044" y="805.104"> 2010;88:887–896G </cdl:metadata><cdl:metadata fname="OFAZVP+HelveticaNeueLTStd-HvEx" fsize="12.0" pagenumber="1" x="186.099" y="805.104">|</cdl:metadata><cdl:metadata fname="OFAZVP+HelveticaNeueLTStd-LtCn" fsize="8.0" pagenumber="1" x="188.763" y="805.104"> doi:10.2471/BLT.09.071852</cdl:metadata><br />
<cdl:metadata fname="PNKFJN+HelveticaNeueLTStd-BdCn" fsize="13.0" pagenumber="1" x="56.693" y="367.697">Introdu</cdl:metadata></cdl:toc></p>
<p><cdl:metadata fname="PNKFJN+HelveticaNeueLTStd-BdCn" fsize="13.0" pagenumber="1" x="56.693" y="367.697">ction</cdl:metadata></p>
如果我使用
editor.insertElement(paragraph);
而不是
editor.insertHtml(paragraph.getOuterHtml()); 然后输出是(从Res中删除p标签)
<p><cdl:metadata fname="PNKFJN+HelveticaNeueLTStd-BdCn" fsize="8.0" pagenumber="1" x="527.063" y="805.082">887</cdl:metadata><cdl:metadata fname="OFAZVP+HelveticaNeueLTStd-LtCn" fsize="10.0" pagenumber="1" x="499.954" y="37.493">Res</cdl:metadata></p>
<p style="-moz-user-select: none;"><cdl:toc pagenumber="1" style="color: blue;"><cdl:metadata fname="OFAZVP+HelveticaNeueLTStd-LtCn" fsize="10.0" pagenumber="1" x="499.954" y="37.493">earch</cdl:metadata><br />
<cdl:metadata fname="OFAZVP+HelveticaNeueLTStd-LtCnO" fsize="8.0" pagenumber="1" x="56.693" y="805.104">Bull World Health Organ</cdl:metadata><cdl:metadata fname="OFAZVP+HelveticaNeueLTStd-LtCn" fsize="8.0" pagenumber="1" x="124.044" y="805.104"> 2010;88:887–896G </cdl:metadata><cdl:metadata fname="OFAZVP+HelveticaNeueLTStd-HvEx" fsize="12.0" pagenumber="1" x="186.099" y="805.104">|</cdl:metadata><cdl:metadata fname="OFAZVP+HelveticaNeueLTStd-LtCn" fsize="8.0" pagenumber="1" x="188.763" y="805.104"> doi:10.2471/BLT.09.071852</cdl:metadata><cdl:metadata fname="PNKFJN+HelveticaNeueLTStd-BdCn" fsize="13.0" pagenumber="1" x="56.693" y="367.697">Introd</cdl:metadata></cdl:toc></p>