jQuery 插件在 ajax 完成回调期间不可用

发布于 2024-11-15 09:05:55 字数 835 浏览 5 评论 0原文

当用户单击链接时,我使用 jQuery fancybox 插件显示一个 iframe,其中包含有关记录的上下文信息。

ajax 成功回调运行时未找到 .fancybox() 插件。类似于以下内容:

$(function() {
    Widgets = {
        show_dialog: function(width, height) {
            $('.fancybox').fancybox({ fancybox settings here});
        }
    }
});

当上面的 Widgets 对象通过检查其后面的断点来定义时,我可以看出 fancybox 已加载并可用。我还可以通过将 show_dialog 函数包装在匿名函数中来修复此函数的调用,该函数的作用域 $ 如下所示:

$(function() {
    Widgets = {
        show_dialog: function($) { return function(width, height) {
            $('.fancybox').fancybox({ fancybox settings here});
        }; }($)
    }
});

更多背景...我正在使用 postMessage jQuery 插件将消息从子框架发送到父框架,如果这很重要(我认为不应该)

任何人都可以解释发生了什么事吗?几乎感觉 $ 在页面执行的后期被重新分配,但我没有在任何地方加载它。

编辑:将 fancybox 的范围更新为 $('.fancybox').fancybox(

I'm using the jQuery fancybox plugin to display an iframe with contextual information about a record when a user clicks on a link.

The .fancybox() plugin is not found at the time the ajax success callback runs. Similar to the following:

$(function() {
    Widgets = {
        show_dialog: function(width, height) {
            $('.fancybox').fancybox({ fancybox settings here});
        }
    }
});

I can tell that fancybox is loaded and available when the above Widgets object is defined from checking a breakpoint just after it. I can also fix the calling of this function by wrapping the show_dialog function in a anonymous function that scopes $ like the following:

$(function() {
    Widgets = {
        show_dialog: function($) { return function(width, height) {
            $('.fancybox').fancybox({ fancybox settings here});
        }; }($)
    }
});

More background... I'm using postMessage jQuery plugin to send the message from a child frame to the parent frame, if that matters (I don't think it should)

Can anyone explain what's going on? It almost feels like $ is reassigned later in the execution of the page but I'm not loading it anywhere.

EDIT: Updated scoping of fancybox to $('.fancybox').fancybox(

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

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

发布评论

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

评论(1

守不住的情 2024-11-22 09:05:55

在我看来,您需要将该调用附加到某种对象,即: $("a.text").fancybox({opts});

您看到的错误是什么?您实际上是从框架页面还是父页面执行此代码吗?

Seems to me like you need to attach that call to an object of some kind, ie: $("a.text").fancybox({opts});

What's the error you're seeing? Are you actually executing this code from the framed page or the parent?

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