CKEditor Inline selection wrapping

你。 提交于 2019-12-04 05:35:33

问题


I'm looking for a way to add a inline span element with attributes to a selection. The hard part of this is getting it working with selections that pass over multiple block level elements.

I was looking in the sourcecode of the StyleCombobox and found this line.

var style = styles[ value ],
elementPath = editor.elementPath();
editor[ style.checkActive( elementPath ) ? 'removeStyle' : 'applyStyle' ]( style );

This way it already works on multiple block level elements.

The only thing is that i would like to apply attributes to the span that is made around the multiple selections for different block level elements instead of applying a style element.

Does anyone know how this can be done?


回答1:


I used this as solution. It is indeed possible to set attributes and element type. this wasn't defined in the api. I found this in the CKEditor 3.0 api (older version)

var style = new CKEDITOR.style({attributes: {name:"changed"}});
editor.applyStyle(style);



回答2:


The latest Solution for your Problem.

Get Selected Text:

editor.getSelection().getSelectedText();

Put tags and attributes

editor.applyStyle(new CKEDITOR.style({
        element : 'span', 
        attributes : {'class':'YourClass','data-Otherattr':'otherattrvalue'}, 
        style : {'background-color':'gray'} 
    });
);


来源:https://stackoverflow.com/questions/15568732/ckeditor-inline-selection-wrapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!