How to “trigger” a click event on a specific anchor element with Javascript? preferably with jQuery

StackOverflow https://stackoverflow.com/questions/2851426

문제

What I want to do is issue a click event, preferably with jQuery, on an anchor element, causing the browser to follow that link. I know that my anchor selectors are correct, as they are returning 1 from .length.

Here's what I've tried:

$('#awesomeLink').click();

But it won't follow the link.

How am I doing it wrong?

Thanks!!

도움이 되었습니까?

해결책

Why not just parse the url and send the user to that link location?

var url = $('#awesomeLink').attr('href');
window.location.href = url;

다른 팁

Try $('#awesomeLink').trigger("click");

Instead of trying to click the link directly, just tell the browser to load a different page:

window.location = "my_link's_URL";

This question is similar to how-do-i-automatically-click-a-link-with-javascript.

If you are just wanting to change the browser location try:

window.location.href = $('#awesomeLink').attr('href');

Otherwise if you are needing a bit more, then this question might help (as might the other questions about native clicking).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top