使用jQuery操作TinyMCE内容

sup*_*rue 9 javascript jquery tinymce

使用TinyMCE,我可以轻松地操作内容并将其发送回编辑器,如下所示:

    // get content from tinyMCE
    var content = tinyMCE.get('content').getContent();

    // manipulate content using js replace
    content = content.replace(/<\/?div>/gi, '');

    // send back to tinyMCE
    tinyMCE.get('content').setContent( content );
Run Code Online (Sandbox Code Playgroud)

上面的代码工作正常.但是,我不能让这个工作:

    // get content from tinyMCE (it provides an html string)
    var content = tinyMCE.get('content').getContent();

    // make it into a jQuery object
    var $content = $(content);

    // manipulate the jquery object using jquery
    $content = $content.remove('a');

    // use a chained function to get its outerHTML
    content = $("<div />").append( $content.clone() ).html();               

    // send back to tinyMCE
    tinyMCE.get('content').setContent( content );
Run Code Online (Sandbox Code Playgroud)

我的方法有问题吗?

sup*_*rue 5

设置和进入 TinyMCE 是正确的;问题在于我的使用.remove()

$content = $content.remove('a');
Run Code Online (Sandbox Code Playgroud)

由于来自 TinyMCE 的内容是单个对象,而不是对象的集合,其中一些是<a>标签,因此该操作无效,并且返回的 html 与原始对象相同。

为了删除链接,我需要这个:

$content = $content.find('a').remove();
Run Code Online (Sandbox Code Playgroud)

我在这个线程中得到了澄清:Difference between $('#foo').remove('a') and $('#foo').find('a').remove()