jQuery live() ...必须单击两次才能激活链接?

发布于 2024-08-25 14:23:49 字数 485 浏览 2 评论 0原文

我有以下代码,简单地说:

$(function() {
  $('a.add-photos-link').live('click', function(e) {
    $(this).colorbox({
      overlayClose: false,
      onComplete: function() {
        $('#add_photos').submit(function(e) {
          // more stuff to do
          e.preventDefault();
        });
      }
    });
    e.preventDefault();
  });
});

但是,这似乎只有在单击链接两次后才起作用。这些链接会动态添加到页面 (a.add-photos-link)。

为什么会发生这种情况?我可以采取什么措施来修复它,以便它在第一次单击后触发?

I have the following bit of code, simply:

$(function() {
  $('a.add-photos-link').live('click', function(e) {
    $(this).colorbox({
      overlayClose: false,
      onComplete: function() {
        $('#add_photos').submit(function(e) {
          // more stuff to do
          e.preventDefault();
        });
      }
    });
    e.preventDefault();
  });
});

However, this only seems to work after single-clicking on the link TWICE. These links are dynamically added to the page (a.add-photos-link).

Why is this happening and what can I do to fix it so it fires after the first single-click?

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

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

发布评论

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

评论(1

原来是傀儡 2024-09-01 14:23:49

您当前的代码仅为链接创建一个颜色框。它不会打开颜色框,这就是为什么您需要单击链接两次:一次创建它,再次打开它。

创建颜色盒时,您可以使用 open 选项(如文档所述)将其打开立即,像这样:

$(this).colorbox({
  open: true,
  overlayClose: false,
  onComplete: function() {
    // ...
  }
});

Your current code only creates a colorbox for the link. It does not open the colorbox, which is why you need to click the link twice: once to create it and again to open it.

You can use the open option (as documented) when creating the colorbox to open it immediately, like so:

$(this).colorbox({
  open: true,
  overlayClose: false,
  onComplete: function() {
    // ...
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文