Simulating a click in jQuery/JavaScript on a link

后端 未结 7 1588
野趣味
野趣味 2020-12-01 04:45

I want to simulate a click on any link on a page using JavaScript. If that link has some function binded to its \'onclick\' event (by any other JS I don\'t have any control

7条回答
  •  星月不相逢
    2020-12-01 05:30

    At first see this question to see how you can find if a link has a jQuery handler assigned to it.

    Next use:

    $("a").attr("onclick")
    

    to see if there is a javascript event assigned to it.

    If any of the above is true, then call the click method. If not, get the link:

    $("a").attr("href")
    

    and follow it.

    I am afraid I don't know what to do if addEventListener is used to add an event handler. If you are in charge of the full page source, use only jQuery event handlers.

提交回复
热议问题