Browser-friendly way to simulate anchor click with jQuery?

前端 未结 4 1708
一整个雨季
一整个雨季 2021-01-01 06:32

I\'m trying to simulate a click on an anchor tag using jQuery. I\'ve been digging around StackOverflow and Google for a while and haven\'t found anything that works on all o

4条回答
  •  一整个雨季
    2021-01-01 07:18

    I tested this in Chrome and Firefox but don't have an IE handy. This should work transparently in most scenarios.

    $('a').live('click.simclick', function(event){
        var $a = $(event.target)
        setTimeout(function(){
            if (event.isPropagationStopped())
                return
    
            $('
    ') .attr({ method: 'get', target: $a.attr('target'), action: $a.attr('href') }) .appendTo(document.body) .submit() // clean up in case location didn't change .remove() }, 0) }) // test it $('a').eq(0).click()

提交回复
热议问题