使用 jQuery 操作 TinyMCE 内容

发布于 2024-12-26 11:15:18 字数 895 浏览 4 评论 0原文

使用 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 );

上面的代码运行良好。但是,我无法让它发挥作用:

    // 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 );

我的方法有问题吗?

With TinyMCE, I can easily manipulate content and send it back to the editor, like this:

    // 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 );

The above code works fine. However, I can't get this to work:

    // 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 );

Is there something wrong with my methodology?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情场扛把子 2025-01-02 11:15:18

设置和访问 TinyMCE 是正确的;问题出在我使用 .remove() 上:

$content = $content.remove('a');

由于 TinyMCE 中的内容是单个对象,而不是对象集合,其中一些对象是 < ;a> 标签,该操作没有效果,返回的 html 与原始的相同。

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

$content = $content.find('a').remove();

我在此线程中收到了澄清: $('#foo').remove('a') 和 $('#foo').find('a').remove() 之间的区别

The setting and getting to TinyMCE was correct; the problem was with my use of .remove():

$content = $content.remove('a');

Since the content from TinyMCE was a single object, and not a collection of objects some of which were <a> tags, that manipulation had no effect, and the html that returned was the same as the original.

In order to remove links, I instead needed this:

$content = $content.find('a').remove();

I received clarification in this thread: Difference between $('#foo').remove('a') and $('#foo').find('a').remove()

时光磨忆 2025-01-02 11:15:18

您需要使用获取编辑器内容

var content = $('#content').html();

并使用设置内容

var content = $('#content').html('<span>NEW CONTENT</span>');

You need to get the editor content using

var content = $('#content').html();

and set the content using

var content = $('#content').html('<span>NEW CONTENT</span>');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文