在编辑器中替换某些内容?

时间:2012-11-10 18:01:32

标签: javascript jquery wordpress iframe tinymce

我已经想过如何使用以下方法在Wpres编辑器中获取所选文本:

我需要用链接替换一些文字作品而不知道如何,任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:1)

如果要在可视化编辑器中获取当前所选文本的文本值:

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

如果您想用新值替换所选文本,除了上述变量外,只需使用它:

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

或者在您想要使用链接的情况下:

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

答案 1 :(得分:0)

使用tinymce API的简便方法:

获得选择

tinymce.get('your_editor_id').selection

更换选择

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