开发者

TinyMCE - Writing plugin that insert content on top

开发者 https://www.devze.com 2023-04-12 16:06 出处:网络
I am trying to write a plugin for TinyMCE with a feature to add content to the beginning of the content. I know the following command which will insert content at the current locati开发者_JAVA百科on o

I am trying to write a plugin for TinyMCE with a feature to add content to the beginning of the content. I know the following command which will insert content at the current locati开发者_JAVA百科on of the cursor. How do I force it to insert it at the beginning of the content?

tinyMCEPopup.editor.execCommand('mceInsertRawHTML', false, "halo world");
tinyMCEPopup.editor.execCommand('mceInsertContent', false, "halo world");


For this you will need to set the cursor location to the beginning of the editor content. you may use the function setCursorLocation:

 ed.selection.setCursorLocation(ed.getBody().firstChild, 0); // node to set the cursor to, second param is offset


The setCursorLocation will move the cursor to the position of the first html element's first content character. This means that if your current html content looks like: <div>abcd</div> then the setCursorLocation will move the cursor between the <div> and the abcd. In some cases this is not acceptable. So another solution would be: ed.setContent('yourText' + ed.getContent()).

0

精彩评论

暂无评论...
验证码 换一张
取 消