How can I trigger the native(href) click of an a tag with jQuery or mootools?
In jQuery trigger('click') and click() seem to only trigger the oncl开发者_StackOverflow中文版ick event. I want to simulate a click of an a tag without an onclick event.
What I am trying to reach is to display a lightbox upon landing. The lightbox is attached to an a tag which needs to be clicked.
Any ideas?
still, to answer your original question, at least insofar as mootools is concerned: you can simulate a click / fire the function set to handle it (if any) via this:
$("someid").fireEvent("click");
The onclick
event is all there is. If it bubbles all the way up without getting stopped, it will bring you to the location of the link.
You might need something to stop it from bubbling. You can either return false;
or e.preventDefault()
to stop the form from submitting, and you can use e.stopPropagation()
and e.stopImmediatePropagation()
in order to stop it from bubbling.
Chances are, there is a way to activate the lightbox plugin without "fake-clicking" a link though.
//I use, when want show image in lightbox by original image or thumbnails list
//simulate click on lightbox image by click another place with same image url (hack pro kliknuti na spravne url lightboxu)
$('#simulate_click').click(function(event){
//get url from simulate link (zjistime url odkazu)
var odkaz = $(this).attr("href");
//look for real lightbox link with the same url (najdeme lightbox odkaz)
var lightOdkaz = $('#rc-carousel ul li a[href="'+odkaz+'"]');
//simulate clik on that lightbox link (klikneme na nej)
lightOdkaz.trigger("click");
//stop propagation of click on original link (zabranime propagaci)
return false;
//event.stopPropagation();
});
Solved it. Slimbox for mootools supports calling the lightbox directly without needing a link.
Most lightboxes do have support to launch them via JavaScript as well as clicking from a link. Slimbox is definitely a nice lightbox, good choice!
Try this:
window.location = $('a#go').attr('href')
精彩评论