【问题标题】:Destroying the Div and its contents inside the jquery dialog when close button is clicked单击关闭按钮时在 jquery 对话框中销毁 Div 及其内容
【发布时间】:2016-05-09 11:57:08
【问题描述】:

我有一个应用程序,当将 csv 数据拖到图形选项卡时,数据将显示为 jquery 对话框扩展内的图形。一切正常。如果我单击关闭按钮,对话框将按预期关闭。但是,使用 chart.js 构建的图形并没有被破坏。就像我在不刷新页面的情况下再次将数据拖放到图形选项卡一样,对话框扩展将打开并构建旧图形。现在,一旦单击关闭按钮,我想销毁 jquery 对话框中的所有 div、画布、图形。我该怎么做?下面是我的代码?

$("#graph_container").dialog("open")
                .dialog({
                    height: height,
                    title: data1 + " " + "(1990 January to 2008 December)",
                    width: width,
                    resizable: true,
                    responsive: true,
                    droppable: true,
                    close: function(ev, ui) {
                           graph_myLine.destroy();
                            $(this).remove();
                   }

                })
                .dialogExtend({
                    "titlebar": 'transparent',
                    "closable": true,
                    "minimizable": true,
                    "minimizeLocation": "right",
                    "icons": {
                        "close": "ui-icon-circle-close",
                        "minimize": "ui-icon-circle-minus",
                        "restore": "ui-icon-circle-triangle-n"
                    },
                    open: function (event, ui) {
                        $('#graph_container').css('overflow', 'hidden'); //this line does the actual hiding
                    }
                });

如果我运行上面的代码,在按下关闭按钮后,如果我再次将数据拖放到图形选项卡,销毁功能在 chart.js 中不起作用。该图仍然存在。它显示错误为 Uncaught TypeError: Cannot read property 'toDataURL' of null 它在下面的代码行中引发错误

$("#graph_container").droppable({
                activeClass: "ui-state-default",
                hoverClass: "ui-state-hover",
                accept: ":not(.ui-sortable-helper)",
                drop: function (event, ui) {
                    HoldOn.open();
                    //alert(ui.draggable.text());
                    // $("#info").html("dropped!");
                    canvas = document.getElementById('canvas');
                    if (canvas.toDataURL() !== document.getElementById('blank').toDataURL())

【问题讨论】:

  • 你的关闭函数在哪里?
  • 抱歉,格式不正确。现在它更新了。可以看到关闭功能。
  • 调用 $(this).empty();在删除之前
  • $("#graph_container").html('');
  • @Anoop 和 sinto:是的,我尝试添加。但 UncaughtTypeError 仍然存在。

标签: javascript jquery chart.js jquery-dialog


【解决方案1】:

尝试将整个渲染空间包装在一个 div 中,并为你的 div 提供 id,假设你的 div 的 id 是graphDiv,然后清空这个 div 中的所有内容你可以写:

$("#graphDiv").empty();

我想这应该可以解决你的问题。

【讨论】:

  • 我尝试通过 $(".col-md-10).empty(); 来清空 div,但它显示上面的“UncaughtTypeError”。
  • 尝试根据 id 而不是按类清空 div,我相信您会收到 UncaughtTypeError 因为它无法检测到您要清空的部分。所以尝试根据清空身份证号。
  • 实际上不,如果我单击关闭按钮,它会清空 div。问题是单击关闭按钮后,如果我再次拖放数据,它将执行我帖子中的第二个代码块,发生错误。
猜你喜欢
  • 2013-04-06
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多