开发者

jquery triggering click event on an anchor

开发者 https://www.devze.com 2023-03-09 01:55 出处:网络
i have something like <a id=\"link\" href=\"home/categoria/\">link</a> and i need to trigger a click on that and am using

i have something like

<a id="link" href="home/categoria/">link</a>

and i need to trigger a click on that and am using

$('#link').trigger('click')
开发者_如何学运维

nothing happens, any help? thanks

P.S.: I can't use location.href because this is for a facebook tab, and it doesn't work


Triggering a click event in jQuery calls any click events that have been attached to the element. It doesn't simulate the user clicking on a link.

In your specific example, you could do:

location.href = $('#link').attr('href');

That would send the browser to the link in question.


It's not possible to fire a link like that (although calling .click() like this will fire anything bound to that event).

If you want to navigate the user somewhere, use window.location instead.


Just use click():

$('#link').click(function(e) {

    e.preventDefault();

    // do some stuff

});

If you want to prevent the default redirect, use the e.preventDefault(); as shown above.

0

精彩评论

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