JQuery click () doesn't click?

I thought jQuery click()could allow us to add a handler or just click an element?

However, I tried:

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

and waited 3 seconds and he won’t click on the first element <a>on the page ... how?

Update: this is due to What is the best way to make autoplay of the Yahoo, jQuery media player? , and therefore be an event handler for clicking on the media, as it turned out .click(), which coincides with trigger('click'), and not disabling, this event handler?

+3
source share
8 answers

click() . click() (). , . , .

+11

. , , "click" jquery. , , <button></button>, .click() , .

"" , , . , href - javascript, ( ):

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

, .first() - .

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

? .first() , , .click() , , ( ).

+1

, ( ):

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

, trigger():

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

? . , , - :

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

    });
0

" . Plz check http://mediaplayer.yahoo.com/api/#example_usage u autoplay = true, .."

- Kai

:

Yahoo, jQuery?


Else for click attach the click handler to what you want, for example:

$('#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?
}

?

0
source

You are trying to use a method click()for something other than its purpose. click()used so that you can catch a click on a specific element, and not to simulate.

More here

-1
source

Source: https://habr.com/ru/post/1752035/


All Articles