Simulate native click

后端 未结 4 1913
灰色年华
灰色年华 2021-01-15 10:30

I need to trigger the click event on a link and then, if none of the listeners prevented the default, change the location.

This is what I\'ve got so far:

         


        
4条回答
  •  旧巷少年郎
    2021-01-15 11:22

    If I understand your question correctly, you have an a element that you don't control, which has an onclick attribute that returns false. Returning false from an onclick handler prevents a click on the link from taking you to the linked page. And you don't want that.

    You could store the onclick handler function and then remove it from the link element:

    var $a = $('#my_special_link'); 
    var onclickHandler = $a.attr('onclick');
    $a.removeAttr('onclick');
    

    Then cal the stored handler function in your custom click handler:

    $a.click(function() {
        // ...
        onclickHandler();
        // ...
    });
    

提交回复
热议问题