当前位置: 动力学知识库 > 问答 > 编程问答 >

Firing native mouse events with Javascript?

问题描述:

I have a situation where I want to trigger mouse events on their equivalent touch events, to deal with mobile compatibility issues.

To do this, I've created event listeners for touch event, that (are supposed to) fire equivalent mouse events on the targets. Unfortunately, I can't figure out a way to fire an event on a dom element.

I was hoping for something like this:

document.body.addEventListener('touchstart', function(event) {

event.target.mousedown()

}, false);

But a function like that doesn't seem to exist. Any solution?

thanks.

网友答案:

To dispatch events manually in JavaScript, use the element.dispatchEvent(evt) method. You can create events to be dispatched using document.createEvent().

Alternatively, if you're using jQuery, you can do this more simply by simply calling $.mousedown() or $.trigger("mousedown").

分享给朋友:
您可能感兴趣的文章:
随机阅读: