Is it possible to simulate isTrusted=true

浪尽此生 提交于 2020-01-15 06:26:30

问题


I want to be able to simulate an isTrusted=true when I call a touchStart event. Is there any library or workaround of any sort to make this possible?

Here is the output when I run the touchStart programmatically vs. when I actually call the touchStart.

I am using mobile safari. According to this site mobile safari does not support it but that can't be true as the output shows the presence of the event. Any help/advice on this would be greatly appreciated. Thank you.


回答1:


Not possible. Events triggered through scripts will always be marked as not trusted (unless using IE). This is for security reasons. In other words event.isTrusted === false if you invoke it through script.

For more info read this.

Pretty sure you are already getting these results but here is an example with clicks. Notice how the first console message is false which is the one that is called by the script:

const element = document.querySelector('div');
const scriptEvent = new Event('click');

element.addEventListener('click', event => console.log(event.isTrusted));

element.dispatchEvent(scriptEvent);
<div>CLICK HERE</div>


来源:https://stackoverflow.com/questions/50073710/is-it-possible-to-simulate-istrusted-true

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