开发者

how to trigger Live() click in jquery

开发者 https://www.devze.com 2023-01-05 10:05 出处:网络
I have an interesting situation. I need to trigger a live click, because simple click doesn\'t work. This is what I have:

I have an interesting situation. I need to trigger a live click, because simple click doesn't work.

This is what I have:

$('.text').trigger('click');

but I need something lik开发者_Python百科e this:

$('.text').trigger(live('click' ...));

or something to fix this problem.

This is my code:

$(".Sets a.largeImage").fancybox({

  'onComplete': function(){
    return errorimage(myurl);
  } 

});

function errorimage(url) {

  $("#fancybox-img").live('click', function(){
   $('.lightbox:first').trigger('click');
  });

  $('#fancybox-img').trigger('click');

}   

The idea is that I want to trigger $('.lightbox:first').trigger('click');, but in live mode, because simple click doesn't work!

Thank you !!!!


The best solution would be to put your click handler in a separate function.

You can then call this function both from the live click handler and when you want to manually trigger the click.


I had the same problem when using Lightbox2 which binds the click event using live(). In my case the solution was to trigger the event like this:

jQuery**('a[rel^="lightbox"]')**.ready(function () {
jQuery("#myimglink").trigger("click");
})

As you can see i was triggering the event only after all the images that are using lightbox are ready. Hope it will help you all - this being my first contribution to stackoverflow :).


I guess the problem you're facing is, that live() and delegate() don't bind an event handler to the DOM element / jQuery object itself, but at some parent node. live() for instance, will bind an handler to the document.body which checks the event.target. In simple words, it makes usage of event bubbling, thats the idea of live events.

In other words, using live, it might be possible to trigger an event like

$(document.body).trigger({ 
   type:   'click',
   target: $('.text')[0]
});

update

unfortunatly, that does not seem to work. I tried to also set the currentTarget and relatedTarget. Where does .live() bind an handler to? somebody?


As I was facing a similar situation, I found SLaks' solution perfectly helpful. However, attaching .live("click", handlerFn) and .bind("click", handlerFn) to the same elements seems to be conflicting (I don't know what precedence rules apply in such a case). But it works fine if you use a custom event instead of "click" for the trigger.

$(".elems").bind("customEvent", handlerFn);
$(".elems").live("click", handlerFn);

$(".elems").trigger("customEvent");

function handlerFn() {
}


This is another way that works for me.

$('#test').live('click', function() {
    alert("It works!");            
});

$('#test')[0].click();

The important part here is:

$('#test')[0].click();


I belive you can just use your first example as trigger is only used to activate a bound element, not to bind it..

So when your do:

$('#someElement').live('click',function(){./*..*/});

this is the same as

$('#someElement').click(function(){./*..*/}); 

apart from the first one also checks for new html on every ajax requests and binds again.

so just do:

$('div.class').trigger('click');

and it should trigger the live elements

0

精彩评论

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

关注公众号