jQuery's click() is not clicking?

邮差的信 提交于 2020-01-14 18:43:32

问题


I thought jQuery's click() can let us add a handler or just click on an element?

However, I tried:

$(function() {
    setTimeout(function() {
        $('a').first().trigger('click'); // or click(), the same
    }, 3000);
});

and waited 3 seconds and it won't click on the first <a> element in the page... how come?

Update: this is related to What is the best way to make the Yahoo media player autostart, jQuery? and so there should already be event handler for clicking on a media, so how come .click(), which is the same as trigger('click'), not firing off that event handler?


回答1:


Calling the click() method does not simulate clicking the link. It calls any click() handlers on the affected element(s). That's a subtle yet important difference. If you want to simulate clicking the link, there is no realiable cross-browser way of doing this.




回答2:


I very recently ran into a similar problem. The reason nothing happens is because anchor tags don't have a "click" method for jquery to call. If you change the anchor tag to a <button></button> tag for instance, the .click() will simulate a user clicking as expected.

There are a few "hacky" workarounds for this, but it would depend how the event handler for the anchor tag is set up. For instance, if there was some javascript inside the anchor tags href attribute, this would work (which happened to be the solution to my problem):

var lnk = $('a.mylink');
window.location = lnk.attr('href');



回答3:


I'm not sure, if .first() is a possible way to use this function. Have you tried

$('a:first').click();

instead? If .first() doesn't exist, it throws an error, the .click() is never reached, and since it lives in an anonymous function, you even might not see the error at all (failing silently).




回答4:


Here's what you could do to simulate a click on a link (as long as these are normal links):

location.href = $('a').first().get(0).href;



回答5:


If you already have an event-handler for the link, you can use the trigger() method:

$('a:first').trigger('click');



回答6:


What exactly do you want to do? A link is usually used to redirect to a new page. So if you need a redirection, use something like this:

        $(function() {
        setTimeout(function() {
            window.location.replace("linkToNewPage.html");
        }, 3000);

    });



回答7:


"There is an option for this. Plz check at this page http://mediaplayer.yahoo.com/api/#example_usage If u set autoplay=true, the first song will be started automatically.."

-Kai

In Relation to:

What is the best way to make the Yahoo media player autostart, jQuery?


Else for a click attach a click handler to what you want e.g:

$('#MyElementID').click(function() {
//How you want it to behave when you click it.
});

window.setTimeout(YourFunction, 3000);

function YourFunction {
//Your function that runs. Maybe fire the onclick handler?
}

?




回答8:


You're trying to use click() method for something else than its purpose. click() is used so you can catch the click on a specific element, not for simulate it.

Read more here



来源:https://stackoverflow.com/questions/3130539/jquerys-click-is-not-clicking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!