English 中文(简体)
jquery fancybox triggered click only working once
原标题:

I am calling a jquery.fancybox link from the click event bound to a table row. The action works fine the first time, however, if I close the fancybox and click again on any row the anonymous function bound to the row still fires, but fancybox doesn t start up. Here s the javascript I m using:

$jq(document).ready(function() {

  $jq( a.edit ).fancybox({
     overlayShow : true,
     hideOnContentClick : false
  });

  $jq( tr ).click(function() {
    alert("clicked");
    $jq(this).find( a.edit ).trigger("click");
  });

});

So, then in the HTML I have anchors classed as "edit":

<tr>
  <td>...</td>
  <td>
    <a href="/candidates/22/qualifications/16/edit" class="edit">edit</a>
  </td>
</tr>

I can always see the alert box, and I can change the trigger / click() call to remove() and it will "work", removing the anchors on multiple occasions. I can also repeatedly manually click the $( .edit ) link itself and it s all good.

So how is it that the anchor click event fires only once when coming in turn from the row click event? Is it something to do with the call I am making to $(this) when describing the function?

问题回答

Give this a shot:

$jq( a.edit ).live( click ,function(){
    $.fancybox( <div>Here is the content</div> {
        overlayShow: true,
        hideOnContentClick: false
    });

    return false;
});

Alternatively, you could do:

 $jq( a.edit ).live( click ,function(){
    $.fancybox({
        href:  some-page.html 
        overlayShow: true,
        hideOnContentClick: false
    });

    return false;
});

Hope that helps!

Try this to debug

$jq( tr ).click(function() {
    // Step 1: make sure the following expression is actually returning elements
    var elements = $jq(this).find( a.edit );

    if ( elements.length ) {
        // Step 2: Just use the `click` method to trigger the event
        alert("Found " + elements.length + "elements");
        elements.click();
    } else {
        alert("No elements found");
    }
});

If all that works, then you can simplify it to

$jq( tr ).click(function() {
    $jq(this).find( a.edit ).click();
});

My guess is that fancybox is trying to show without the click event. After fancybox shows, you trigger a click outside of fancybox which closes it before it has a chance to show. Put the fancybox link outside of the table and then when tr is clicked, trigger the anchor click which will pop open fancy box.

If this problem persist, you may want to throw some debug statements inside the fancybox library to track down the issue.

Did you solve this problem? If didn t, there is a tricky way to solve this. I think the trigger() method will be unbinded when you open once and close the box. I added hidden elements every click events:

function openPopup(url) {
    $("body")
    .append("<a style= display:none ></a>")
    .fancybox({
         href  : url
    })
    .trigger( click );
}

Hope this helps

Tuffkid got it working; I was working on something to fix this issue too and I came to the same conclusion he did; just sharing, anyway.

On load:

$(document).ready(function(){
    $( #show ).click(function(e){
        showPopUp($(this));
    });
});

JS Function:

function showPopUp(obj){
    obj.fancybox({      
         href  :  page.php ,
         onCleanup : function(){
             $(this).click(function(e) { showPopUp($(this)); });
    }
            });
}

jquery fancybox trigger working only once..

Normally we are use like this $(".fancybox").trigger( click ); but its not working properly.

Just change your tigger like this... its working for me ...

 $(".fancybox").fancybox().trigger( click );

Here at first find the fancybox then trigger it

I did add

elem.removeData( "fancybox" ); 

after

.trigger( click );

It worked for me.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签