问题
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