开发者

jQuery live() ... have to click twice to activate links?

开发者 https://www.devze.com 2022-12-23 08:09 出处:网络
I have the following bit of code, simply: 开发者_Python百科$(function() { $(\'a.add-photos-link\').live(\'click\', function(e) {

I have the following bit of code, simply:

开发者_Python百科$(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?


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() {
    // ...
  }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号