This article is automatically translated by LLM, so the translation may be inaccurate or incomplete. If you find any mistake, please let me know. You can find the original article here .
In my spare time, I tried to implement a simple right-click menu using pure JavaScript. Although I didn't use any complex CSS and the appearance is quite ugly, it is basically functional.
Here you will see that both contextmenu and click do the same thing, which is to hide the menu.
If stopPropagation() was not called in the previous step, clicking on the specified element would also remove the show class.
document.addEventListener('contextmenu', e => { ctxmenu.classList.remove('show')})document.addEventListener('click', e => { ctxmenu.classList.remove('show')})