我想在1秒钟的延迟后显示元素的contextmenu
(就像按下鼠标右键一样)。我到目前为止所做的:
1)试图用诺言延迟执行回调-不起作用,contextmenu
显示为异步且与回调执行无关。
p.addEventListener('contextmenu', e => { // contextmenu is shown instantly
new Promise(resolve => setTimeout(resolve, 1000))
.then(() => console.log('1 s passed')) // shown in 1 s
})
<p id="p">Hello</p>
2)我自己调度了new Event
-在这种情况下根本没有contextmenu
enter code here
p.addEventListener('mousedown', e => {
const contextmenu = new MouseEvent('contextmenu')
setTimeout(() => p.dispatchEvent(contextmenu), 1000)
})
p.addEventListener('contextmenu', e => {
console.log('contextmenu event arrived') // message is shown in 1 s but no contextmenu
})
<p id="p">Hello</p>
检查此项目(https://swisnl.github.io/jQuery-contextMenu/demo.html)。实现上下文菜单的工具之一。这样,您就可以完全控制上下文菜单的行为。