简体   繁体   English

TinyMCE编辑器中的目标元素

[英]Target elements in TinyMCE editor

What is the best way to target elements (say all images of a given class) in the TinyMCE 4 editor. 在TinyMCE 4编辑器中定位元素(比如给定类的所有图像)的最佳方法是什么。 I found some older solutions (ie Adding hover event to elements inside a tinymce editor ), however, they apply to TinyMCE 3. Note that elements can be added to the editor after initial rendering, so it would need something like jQuery's on() functionality. 我发现了一些较旧的解决方案( 即将悬停事件添加到tinymce编辑器中的元素 ),然而,它们适用于TinyMCE 3.请注意,元素可以在初始渲染后添加到编辑器中,因此需要类似jQuery的on()功能。

One option might be to to do something like $('#tinymce_id').contents()... . 一种选择可能是做一些像$('#tinymce_id').contents()...

Or maybe when configuring TinyMCE, tinymce.init({'selector': '#tinymce_id','setup' : function(ed) {do something here?}}); 或者也许在配置TinyMCE时, tinymce.init({'selector': '#tinymce_id','setup' : function(ed) {do something here?}});

The best way I found to do it. 我找到的最佳方式。

tinymce.init({
    'selector': "#tinymce_id",
    'setup' : function(ed) { 
        ed.on('init', function(e) {
            $(ed.getBody()).on("click", "img", function() {alert('hello');});
        });
    }
});

Use this logic: 使用此逻辑:

  • Get TinyMCE text 获取TinyMCE文本
  • Convert it to a DOM (HTML) element 将其转换为DOM(HTML)元素
  • Select anything using jquery 使用jquery选择任何内容
  • Modify or do what you want with those elements selected 使用所选元素修改或执行所需操作
  • Convert the html back to text 将html转换回文本
  • set the content of the TinyMCE editor to the new html 将TinyMCE编辑器的内容设置为新的html


//on click event
$("#test").click(function(){
    //get the text of the tinymce editor and convert it to an html element
    html = $.parseHTML(tinymce.activeEditor.getContent());

    //do anything with the content here
    $(html).find("img.editor-img").css("width", "100px");

    //convert it back to text
    text = $(html).html();
    //add it to the tinymce
    tinymce.activeEditor.setContent(text);
});

Example: http://jsfiddle.net/rqwVA/1/ 示例: http//jsfiddle.net/rqwVA/1/

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

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