Javascript 如何彻底销毁tinymce?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2663844/
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-23 01:22:04  来源:igfitidea点击:

How to destroy tinymce completely?

javascripttinymce

提问by powerboy

I am working on something like this:

我正在做这样的事情:

On a webpage, there is an article wrapped in a DIV, an Edit button. When a user click on the Edit button, insert an textarea via javascript, load the html of the DIV into the textarea, load and initial tinymce. When the user click on the Save button, save and update the article via ajax, and destroy tinymce completely.

在网页上,有一个包裹在 DIV 中的文章,一个编辑按钮。当用户点击编辑按钮时,通过javascript插入一个textarea,将DIV的html加载到textarea中,加载并初始化tinymce。当用户点击保存按钮时,通过ajax保存并更新文章,并彻底销毁tinymce。

The problem is that, I failed to destroy tinymce. Here is the doc of the destroy method.

问题是,我没有销毁tinymce。这是destroy 方法的文档

I am using the jQuery version of tinymce, the lastest V3.2.2

我使用的是 jQuery 版本的 tinymce,最新的 V3.2.2

Here is the sample code:

这是示例代码:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
        <script type="text/javascript" src="../js/tinymce/jquery.tinymce.js"></script>
        <script type="text/javascript">
            $(function() {
                $('button.load').click(loadTinyMCE);
                $('button.destory').click(destoryTinyMCE);
            });

            function loadTinyMCE() {
                $('textarea').tinymce({
                    script_url : '../js/tinymce/tiny_mce.js'
                });
            }

            function destoryTinyMCE() {
                $('textarea').tinymce().destroy();
            }
        </script>
    </head>
    <body>
        <textarea>abc</textarea>
        <button type="button" class="load">Load TinyMCE</button>
        <button type="button" class="destory">Destory TinyMCE</button>
    </body>
</html>

回答by Ethan

Use remove() instead.

请改用 remove()。

回答by John K

You already have a practical accepted solution with remove() so here's an answer about destroy().

您已经有一个使用 remove() 的实用解决方案,所以这里有一个关于 destroy() 的答案。

Destroy methods (sometimes called dispose) are common in many programming languages to allow clean up of resources used by an instance of something. Destroy is conventionally a pure memory concept (i.e. non-visual). The destroy method is often different than an object-oriented destructor method by lifetime in that destroy is intended to be allowed to be called explicitly by the programmer in order to clean up memory and resources before the final destructor runs or before the garbage collector comes along (in either case earlier, so you can release stuff when you don't need it any longer instead of letting those resources be used until the end of the program). Sometimes the destructor method will naturally contain a call to the destroy method, to ensure any resources are finally cleaned up, so usually the programmer doesn't have to worry about not calling it because it will eventually be called automatically.

Destroy 方法(有时称为 dispose)在许多编程语言中很常见,以允许清理某物实例使用的资源。销毁通常是一个纯粹的记忆概念(即非视觉)。在生命周期上,destroy 方法通常不同于面向对象的析构函数方法,因为它旨在允许程序员显式调用 destroy 以在最终析构函数运行之前或垃圾收集器出现之前清理内存和资源(无论在哪种情况下,您都可以在不再需要时释放资源,而不是让这些资源一直使用到程序结束)。有时析构函数方法自然会包含对destroy方法的调用,以确保最终清理任何资源,因此通常程序员不会

Often the destroy method code body is provided by the programmer for application-specific behaviour (overridden from the base class/object in some languages). This means that calling destroy without having provided an implementation for it will often do nothing - an empty code body. Of course for TinyMCE it will have implemented its own destroy methods appropriately.

通常,destroy 方法代码体由程序员为特定于应用程序的行为提供(在某些语言中从基类/对象覆盖)。这意味着在没有为其提供实现的情况下调用 destroy 通常不会做任何事情 - 一个空的代码体。当然,对于 TinyMCE,它会适当地实现自己的销毁方法。

The TinyMCE documentation doesn't promise any visual changes upon destroy, only that the instance will be stripped of memory leaking possibilities. This is in line with what destroy methods commonly do.

TinyMCE 文档不承诺销毁后的任何视觉变化,只是实例将被剥夺内存泄漏的可能性。这与销毁方法通常所做的一致。

Destroys the editor instance by removing all events, element references or other resources that could leak memory. This method will be called automatically when the page is unloaded but you can also call it directly if you know what you are doing.

通过删除所有可能泄漏内存的事件、元素引用或其他资源来销毁编辑器实例。这个方法会在页面卸载时自动调用,但如果你知道自己在做什么,也可以直接调用它。

This is also why TinyMCE provides a remove() method to visually change things, because destroy() is not intended to carry out the exact same purpose.

这也是 TinyMCE 提供 remove() 方法来直观地更改事物的原因,因为 destroy() 并非旨在执行完全相同的目的。

In order to destroy TinyMCE completely you might issue remove() for visual cleanup followed by dispose() for memory cleanup; however those methods are implementation specific and I'm unsure how TinyMCE would react.

为了完全销毁 TinyMCE,您可以发出 remove() 进行可视化清理,然后发出 dispose() 进行内存清理;但是这些方法是特定于实现的,我不确定 TinyMCE 会如何反应。

回答by Gurpreet

hi my problem is solved by using this

嗨,我的问题通过使用这个解决了

Toggle one or many TinyMCE instances using:

使用以下方法切换一个或多个 TinyMCE 实例:

$('.editors').tinymce('toggle');

$('.editors').tinymce('toggle');

Or remove instances entirely using:

或者使用以下方法完全删除实例:

$('.editors').tinymce('remove');

$('.editors').tinymce('remove');

from http://mktgdept.com/jquery-tinymce-plugin

来自 http://mktgdept.com/jquery-tinymce-plugin

回答by Jared Forsyth

To 'destroy' an element in jquery, use $(node).remove(). If this doesn't work, a code example will help us understand/answer your question better.

要“销毁”jquery 中的元素,请使用$(node).remove(). 如果这不起作用,代码示例将帮助我们更好地理解/回答您的问题。