How to use jquery trigger anchor's default click event?

前端 未结 5 1823
醉话见心
醉话见心 2021-01-06 12:57

Suppose I have an anchor in my page like :turn to header then I also set a button in my page

相关标签:
5条回答
  • 2021-01-06 13:18

    Try This, working for Me !!

    $("#anchor").get(0).click();
    

    This will call default click of the anchor.

    0 讨论(0)
  • 2021-01-06 13:18

    jQuery does not have a way to trigger the native DOM event directly.

    To do that, you can use the plain old standard DOM methods:

    var evt = document.createEvent("Event");
    evt.initEvent("click", true, true);
    $('#anchor').get(0).dispatchEvent(evt);
    

    For support of IE8 or earlier, you need to use the IE-specific event methods createEventObject and fireEvent.

    0 讨论(0)
  • 2021-01-06 13:32

    You can Also use to trigger click/any events

    $('#bycategory').click(function(){
      alert('triggered');
    });
    $('#bycategory').triggerHandler("click");
    
    0 讨论(0)
  • 2021-01-06 13:33

    You have a spelling error. Please use:

    {$("#anchor").trigger("click");});
    
    0 讨论(0)
  • 2021-01-06 13:36

    Please see this answer.

    Instead of trying to trigger the click event, try setting the window.location property:

    window.location = '#header';
    

    Or perhaps:

    $("input").click(function() {
      window.location = $('#anchor').attr('href');
    });
    
    0 讨论(0)
提交回复
热议问题