通过jquery访问tinymce iframe元素

Moh*_*Ram 3 jquery tinymce

我使用Tinymce编辑器.需要使用jquery访问tinymce iframe我试过...

var iframe = $('#comment_ifr')[0];//document.getElementById('comment_ifr');// or $('#comment_ifr')[0]; the other is just faster.
alert(iframe);
var doc = iframe.document || iframe.contentDocument || iframe.contentWindow && iframe.contentWindow.document || null;
   //if( !doc ) return;
   //and now jquery

$( "img", doc ).click(function() {
    alert('image clicked');
   });

----------
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,一旦图像插入了tinymce iframe.一旦我点击该图像,我需要触发一个事件.帮我.

Tha*_*ama 5

您可以使用以下命令更轻松地访问iframe文档:

var doc = tinymce.get('comment').getDoc();
Run Code Online (Sandbox Code Playgroud)

编辑: 为了达到你想要的效果,你可以在tinymce中捕捉点击事件并用它做你想做的事.您需要将此代码插入到自己的tinymce插件中或使用tinymce init参数:

ed.onClick.add(function(ed, evt){

    // Firefox
    if (evt.explicitOriginalTarget){ // this is the img-element
      if (evt.explicitOriginalTarget.nodeName.toLowerCase() == 'img'){
        console.log(evt.explicitOriginalTarget);
        alert('image clicked');
      }
    }
    // IE
    else if (evt.target) { // this is the img-element
      if (evt.target.nodeName.toLowerCase() == 'img'){
        console.log(evt.target);
        alert('image clicked');
      }
    }
}); // end click event
Run Code Online (Sandbox Code Playgroud)