Javascript 如何动态替换 TinyMCE 中的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8505776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:18:09  来源:igfitidea点击:

How to dynamically replace content in TinyMCE?

javascripthtmltinymce

提问by Peter

I want to replace all {baseurl}keywords to proper url in TinyMCE editor. How can i do that?

我想{baseurl}在 TinyMCE 编辑器中将所有关键字替换为正确的 url。我怎样才能做到这一点?

For example if user will add HTML in editor <img src="{baseurl}/image.jpg" />i want to see this image in TinyMCE editor - so this will be replaced to <img src="http://mydomain.com /image.jpg" />

例如,如果用户将在编辑器中添加 HTML,<img src="{baseurl}/image.jpg" />我想在 TinyMCE 编辑器中看到这个图像 - 所以这将被替换为<img src="http://mydomain.com /image.jpg" />

Any ideas?

有任何想法吗?

回答by Thariama

Here is the code that will replace your editor content. But you will need to do this action at the correct time.

这是将替换您的编辑器内容的代码。但是您需要在正确的时间执行此操作。

var editor = tinymce.get('my_editor_id'); // use your own editor id here - equals the id of your textarea
var content = editor.getContent();
content = content.replace(/{$baseurl}/g, 'http://mydomain.com');
editor.setContent(content);

回答by jaheraho

With this solution I was able to modify the content on-the-fly, without replacing the content as whole:

使用此解决方案,我能够即时修改内容,而无需替换整个内容:

tinymce.init({
   setup: (editor)=>{
      editor.on('init', ()=>{
         $(editor.contentDocument).find('a').prop('title', 'my new title');
      });
   }
});

Maybe it helps someone :)

也许它可以帮助某人:)

回答by Abdo-Host

I used a very simple code working good with me

我使用了一个非常简单的代码,对我很好

tinymce.get("page-content").setContent(''); // 'page-content' as the textarea id
tinymce.get("page-content").execCommand('mceInsertContent', !1, 'New content data');