Difference between .click() and creating a mouse e

2019-03-27 11:43发布

问题:

So I am trying to work out the differences between

link.click()

and

var event = document.createEvent("MouseEvents");
event.initEvent("click", true, false);
link.dispatchEvent(event);

As far as I can tell these should be the same things (however working with my jsfiddle example of exporting a csv from a URI this is not the case as they perform differently from browser to browser)

Using .click() with firefox the popup to download the csv will not show (it will in chrome)

see example -> http://jsfiddle.net/a5E9m/23/

Where as using the Mouse events it will

see example -> http://jsfiddle.net/a5E9m/25/

回答1:

I think that Firefox has restrictions around the click function on an <a> element. See here. Whereas when you wire up the mouse event yourself, you are manually adding the click wiring. Also, see here and here.

Also, as Boris Zbarsky pointed out in the comments, the <a> element does not have a click function on it in the spec.