Using jQuery to programmatically click an link

后端 未结 9 1934
一生所求
一生所求 2020-11-29 03:55

I know this question has been asked before, but after a search on the web I can\'t seem to find a straight forward answer.

the HTML

         


        
9条回答
  •  执念已碎
    2020-11-29 04:18

    Click just triggers the click event / events not the actually "goto-the-links-href" action.

    You have to write your own handler and then your $('#myAnchor').trigger('click'); will work...

    $("#myAnchor").click(function(event)
    {
      var link = $(this);
      var target = link.attr("target");
    
      if($.trim(target).length > 0)
      {
        window.open(link.attr("href"), target);
      }
      else
      {
         window.location = link.attr("href");
      }
    
      event.preventDefault();
    });
    

提交回复
热议问题