jQuery 插件在 ajax 完成回调期间不可用
当用户单击链接时,我使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,您需要将该调用附加到某种对象,即:
$("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?