Facebox 只能使用一次
我正在按照 Facebox 网站上的文档说明绑定我的 Facebox 请求。
但当我点击 div.comment 之一后,Facebox 请求不再起作用。
我正在使用的代码就在下面,再往下是我的错误。
$(document).ready(function() {
$('.comment').bind('click', function() {
$.facebox({ajax: '/project/cake_app/comment/tweets/' + $(this).attr('id')});
});
});
错误:
Uncaught TypeError: Object function (a,b){return new d.fn.init(a,b,g)} has no method 'facebox'
Am binding my Facebox requests just how the documentation says on the Facebox website.
But after I click one of the div.comment's the Facebox request doesn't work again.
The code I am using is just below and further down is my error.
$(document).ready(function() {
$('.comment').bind('click', function() {
$.facebox({ajax: '/project/cake_app/comment/tweets/' + $(this).attr('id')});
});
});
Error:
Uncaught TypeError: Object function (a,b){return new d.fn.init(a,b,g)} has no method 'facebox'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Kim 建议用 live() 替换 bind() 不起作用的原因是因为问题不在于原始单击事件处理程序丢失。
正如错误所示:
问题是 jQuery $ 对象丢失了 .facebox() 方法。 Facebox 可以正常关闭和重新打开窗口,除非您在 Facebox 内加载 jQuery。这样做会在加载 Facebox 后重新初始化 jQuery,因此当 jQuery 重新初始化时,Facebox 没有机会将自身重新添加到 jQuery $ 对象中。
我通过从 Facebox 中加载的代码中删除 jQuery 解决了这个问题。如果 jQuery 已经加载到主页上,那么它就在环境中,不需要在 Facebox 代码中再次加载它。如果您在主页和 Facebox 内的代码中都依赖 jQuery,这只是一个问题。我想如果您必须在 Facebox 内再次加载 jQuery,您可以使用 jQuery.noConflict() 来使用单独的实例。
The reason Kim's suggestion to replace the bind() with live() didn't work is because the problem is not that the original click event handler gets lost.
As the error suggests:
The problem is that the jQuery $ object loses the .facebox() method. Facebox works fine closing and reopening the window EXCEPT when you load jQuery INSIDE the facebox. Doing that reinitializes jQuery after facebox has already been loaded, so facebox doesn't have a chance to re-add itself to the jQuery $ object when jQuery reinitializes.
I solved this problem by removing jQuery from the code which loads in the facebox. If jQuery is already loaded on the main page, then it is in the environment and there is no need to load it again inside the facebox code. This is only an issue if you rely on jQuery in both your main page AND in the code inside the facebox. I suppose if you HAVE to load jQuery again inside the facebox, you can use a seperate instance by using jQuery.noConflict().
您是否尝试过使用 live 这样的方式:
have you tryed using live like:
希望它有效
hope that it works
我通过从 Facebox 切换到 Fancybox 解决了我的问题,这似乎是 Facebox 的一个错误,在使用一次后会解除事件的绑定。
I solved my problem by switching from Facebox to Fancybox, it seems their is a bug with Facebox that is unbinding the event after it is used once.