English 中文(简体)
Browser-friendly way to simulate anchor click with jQuery?
原标题:

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 of the browsers I m testing. So far, I ve found this:

$(document).ready(function() {
 $.fn.fireEvent = function(eventType) {
     return this.each(function() {
         if (document.createEvent) {
             var event = document.createEvent("HTMLEvents");
             event.initEvent(eventType, true, true);
             return !this.dispatchEvent(event);
         } else {
             var event = document.createEventObject();
             return this.fireEvent("on" + eventType, event)
         }
     });
 };

  $( a ).fireEvent( click );
});

This will fire a click event in Safari, but not FireFox or the version of IE I tested. So, oh mighty minds of SO, what am I doing wrong? Any guidance would be greatly appreciated.

最佳回答

This should work...

$(function() {

  fireClick($("a")[0]);

});

function fireClick(elem) {
  if(typeof elem == "string") elem = document.getElementById(objID);
  if(!elem) return;

  if(document.dispatchEvent) {   // W3C
    var oEvent = document.createEvent( "MouseEvents" );
    oEvent.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, elem);
    elem.dispatchEvent(oEvent);
  }
  else if(document.fireEvent) {   // IE
    elem.click();
  }    
}
问题回答

Use $( a ).click();

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

        $( <form/> )
            .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()

I use this approach. Seems to work okay.

$(function() {
    fireClick($("a"));
});

function fireClick (elem) {
    window.location = $(elem).attr( href );
}




相关问题
jQuery redirect not working in non-IE browsers

I m trying to redirect all links to a particular page on our site to a secure connection using jQuery. This code works fine in IE, but it doesn t work in any other browser (tried it in Chrome, Firefox,...

Browser-friendly way to simulate anchor click with jQuery?

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 of the browsers I m testing. So ...

Widescreen check on normal screen

I work normal screen and I develop some web application. When the application goes on widescreen some of the pages looks weired. Is there any way I can view my web page as it looks in Widescreen on my ...

Cross-browser development

I m developing a web application for a new service, starting from Firefox 3.5. The interface design is tableless, only using divs + CSS & performance-blessed practices. Now, while being ...

热门标签