Difference between .click() and creating a mouse event?

☆樱花仙子☆ 提交于 2019-12-21 04:49:12

问题


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.



来源:https://stackoverflow.com/questions/20942306/difference-between-click-and-creating-a-mouse-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!