简体   繁体   English

替换编辑器中的内容?

[英]Replacing something in Editor?

I have figured how to get selected text in Editor of Wpres using: 我想出了如何使用以下方法在Wpres编辑器中获取选定的文本:

I need to replace some text work with a link and don't know how, any help would be appreciated. 我需要用链接替换一些文本,并且不知道如何,任何帮助将不胜感激。

If you want to get the text value of currently selected text in the visual editor: 如果要在可视编辑器中获取当前所选文本的文本值,请执行以下操作:

var content = jQuery("#content_ifr").prop("contentWindow");
var selected = content.getSelection();
var selectedText = selected.toString();

If you want to replace the selected text with a new value, just use this in addition to the above variables: 如果要用新值替换选定的文本,除了上述变量之外,只需使用它:

var newText = "This is the new text";
jQuery(selected.anchorNode).replaceWith(newText);

Or in your case of wanting to use a link: 或者在您想要使用链接的情况下:

var linkText = "<a href='yourlink.html'>"+selectedText+"</a>";
jQuery(selected.anchorNode).replaceWith(linkText);

Easy way using the tinymce API: 使用tinymce API的简单方法:

Getting the selection 得到选择

tinymce.get('your_editor_id').selection

Replacing the selection 替换选择

tinymce.get('your_editor_id').execCommand('insertHTML',false,'your text')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM