javascript - Ajax 后 Fancybox 链接不起作用

标签 javascript jquery ajax load fancybox

我已经创建了 fancybox 表单:

$("a.productForm").fancybox({
    'hideOnOverlayClick' : true,
    'modal' : true,
    'hideOnContentClick' : false,
    'showCloseButton' : false,
    'padding' : 0,
    'transitionIn'  :   'elastic',
    'transitionOut' :   'elastic',
    'autoScale'     : false,
    'autoDimensions': false,
    'frameWidth': 717,
    'frameHeight': 900,
    'overlayOpacity': 0.4,
    'centerOnScroll' : false,
    'type': 'ajax',
    'ajax': {
    dataFilter: function(data) {
        return $(data).find('#form_wrapper')[0];}
}
}); 

您可以在 this 中看到页面它是如何工作的。

要查看我的 fancybox 表单,请按绿色按钮:

enter image description here

正如您所看到的,此页面中的所有内容都运行良好,但我的代码无法在使用 ajax 加载产品的页面中运行。您可以看到this页。

当您按下此页面中的绿色按钮时,fancybox不会显示,而是直接转到链接。可能出了什么问题?

也许我必须在使用 ajax 加载此链接后将 fancbox 代码应用于该链接?

最佳答案

在接收到 html 数据并将其添加到 dom 后,您需要在 ajax 成功回调中执行 $("a.productForm").fancybox(...) 。

这是一个简单的例子:

$.ajax({
  url: "test.html",
  success: function(data){
    $('#newest_desc').html(data); // Here you insert data which I'm expecting to be a string with html into the tab with the id "newest_desc". I got that from your website. Not sure if it's correct though.
    $("a.productForm").fancybox(...) // Here is where you call the fancybox function because now you know that the element a.productForm is available.
  }
});

关于javascript - Ajax 后 Fancybox 链接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9567949/

相关文章:

javascript - 如何正确处理textarea中的html标签

jQuery Fancybox 触发器

javascript - 是否可以在选择时绑定(bind)选择选项而不是在更改时绑定(bind)

javascript - 找不到代码中的语法错误

javascript - SmtpJs API 不工作!有什么方法可以使用带有 JavaScript 或 JQuery 的 SMTP 服务器发送电子邮件

javascript - 在 javascript 中使用的引用在 catch-Block 中不可用

javascript - Bootstrap 模式未显示

javascript - 高效使用 async/await

javascript - rails 4 中的 Ajax 请求更新值

jquery - 如何仅显示 Twitter 列表中的成员列表及其头像