How to programmatically trigger the click on a link using jQuery?

前端 未结 4 1053
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 14:18

How to programmatically trigger the click on a link using jQuery?

相关标签:
4条回答
  • 2020-11-28 14:40
    $('#your_link_id')[0].trigger('click');
    

    is needed as jQuery returns an array and we cannot trigger click on multiple eventlinks. You have to target only a single element

    0 讨论(0)
  • 2020-11-28 14:42

    You can use trigger:

    $('#your_link_id').trigger('click');
    
    0 讨论(0)
  • 2020-11-28 14:47

    If you have an anchor link:

    <a id="my_link_id" href="something">My Link</a>
    

    it will fail as other answers have mentioned. Calling .eq and .trigger('click') doesn't work for me, but this does:

    $('#your_link_id').get(0).click();
    

    In my specific case, I've assigned a blob url programmatically to the anchor's href.

    0 讨论(0)
  • 2020-11-28 14:50
    $('#your_link_id').click()
    

    See the excellent jquery docs for more information

    0 讨论(0)
提交回复
热议问题