当单击特定的 HTML 元素时,我希望在上下文菜单中显示一个自定义元素,单击该元素会调用 Javascript 函数。
// The element to whose context menu the item should be added.
const element = document.getElementById('example');
// The menu item that should be added.
const menuItem = document.getElementById('menuItem');
element.addEventListener('contextmenu', e => {
//e.menuItems.add(menuItem); // How to get it into the menu?
});
#example{border:thin solid;padding:1em;}
<p id="example">
I want to add an item to the context menu
hat opens on this paragraph.
</p>
<!--
the element below is supposed to show
in the context menu instead of inline.
-->
<span
onclick="javascript:alert('the menu item has been clicked')"
id="menuItem">
Click me!
</span>
慕无忌1623718
互换的青春
函数式编程
相关分类