jQuery:是否有任何理由不将 DOM 元素存储在变量中而不是将它们附加到 DOM?

发布于 2024-09-18 15:19:59 字数 1095 浏览 7 评论 0原文

我正在开发一个插件,它可以获取远程页面的一部分,并保存该部分以便在 jQuery UI 对话框小部件中使用;最初我将 HTML 附加到 DOM 并用 display:none 隐藏它,直到用 $.dialog 调用它,但我意识到我可以将 DOM 节点留在多变的。

有什么理由不这样做呢?这是一个例子:

function makeDialogLink() {

    var HTML = $("<div />").load('file.html #container', function() {

        $('a#test').bind('click', function(e) {
            e.preventDefault();
            showDialog();
        }); 
    });

    function showDialog() {
        $(HTML).dialog({
            autoOpen : true,
            width  : opts.width,
            modal  : opts.modal,
            resizable : opts.resizeable,
            title  : opts.title
        });

        // some other stuff happens in here, and a setTimout
        closeDialog();
    }
    function closeDialog() {
        $(HTML).dialog('close');
    }
}

我已经从我实际做的事情中简化了很多,但这很好地概括了我的问题。

正如您所看到的,我已经加载了远程文档的一部分,我将其弹出一个对话框,然后关闭该对话框,但我从未将获取的 HTML 直接附加到 DOM ($.dialog 当然,在某个时候会这样做)。

有什么理由不这样做吗?看起来比将 HTML 放入隐藏的 DIV 中然后稍后再获取要好得多。我只是想知道以这种方式在 javascript 中使用闭包是否存在一些我不知道的陷阱。

I've got a plug-in I'm working on which fetches a portion of a remote page, and saves that portion to be used in a jQuery UI dialog widget; originally I was attaching the HTML to the DOM and hiding it with display:none until it was called with $.dialog but I realized I could just leave the DOM node in a variable.

Is there any reason not to do this? Here's an example:

function makeDialogLink() {

    var HTML = $("<div />").load('file.html #container', function() {

        $('a#test').bind('click', function(e) {
            e.preventDefault();
            showDialog();
        }); 
    });

    function showDialog() {
        $(HTML).dialog({
            autoOpen : true,
            width  : opts.width,
            modal  : opts.modal,
            resizable : opts.resizeable,
            title  : opts.title
        });

        // some other stuff happens in here, and a setTimout
        closeDialog();
    }
    function closeDialog() {
        $(HTML).dialog('close');
    }
}

I've simplified this quite a bit from what I'm actually doing, but this encapsulates what my question is about pretty well.

As you can see I've loaded a portion of a remote document, I pop it up in a dialog, and close that dialog later, but I never directly attach the fetched HTML to the DOM ($.dialog does it at some point, of course).

Is there any reason to NOT do it this way? Seems a lot nicer then having to shove the HTML into a hidden DIV and then fetch it later. I'm just wondering if there's some pitfall I'm unaware of to using closures in javascript in this fashion.

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

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

发布评论

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

评论(3

漆黑的白昼 2024-09-25 15:19:59

我想不出这有什么严重的缺点。它可能会慢一点,因为每次您需要对话框时它都会将其重新附加到 DOM,但这并不重要,因为 1) 差异甚至不会明显,除非您弹出数百个对话框 2) 它如果有的话,可能不会比切换 div 慢多少。

这似乎是最好、最干净的解决方案。如果我处于你的位置,我会保留它。

I can't think of any serious drawbacks to that. It may be a bit slower, since it's reattaching it to the DOM every time you need a dialog but it doesn't really matter since 1) the difference would not even be noticeable unless you're popping up hundreds of dialogs and 2) it may not be that much slower, if at all, than having a div that you toggle.

It seems like the best and cleanest solution. I would keep it if I were in your position.

囍孤女 2024-09-25 15:19:59

是的,您必须小心未附加到 DOM 的节点。 IE 因内存泄漏而臭名昭著。 (向下滚动到跨页泄漏部分。)

由于您正在创建一个独立的 DOM 树,然后将其附加到文档(在 showDialog() 中),我想您的代码泄漏。当然,唯一确定的方法是获取一些分析工具并找出答案。

更安全的方法是使用隐藏的 div

var HTML = $("#hiddenDiv").load('file.html #container');

Yes, you have to be careful about nodes which are not attached to the DOM. IE is notoriously bad at leaking memory. (Scroll down to the section on cross-page leaks.)

Since you're creating an unattached DOM tree and then attaching it to the document (in showDialog()), I'd imagine your code leaks. Of course, the only way to be sure is to get some profiling tools and find out.

A safer way to do this is to use a hidden div.

var HTML = $("#hiddenDiv").load('file.html #container');
挽心 2024-09-25 15:19:59

您还可以将 ajax 调用中的数据存储为字符串(使用 $.get 或 $.ajax 代替)。仅在用户单击对话框按钮后创建 dom 元素。

You can also just store the data from your ajax call as a string (use $.get or $.ajax instead). Only create the dom elements after the user clicks on the dialog button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文