contextmenu - HTML: HyperText Markup Language 编辑
The contextmenu attribute is obsolete and will be removed from all browsers
The contextmenu
global attribute is the id of a <menu>
to use as the contextual menu for this element.
A context menu is a menu that appears upon user interaction, such as a right-click. HTML5 now allows us to customize this menu. Here are some implementation examples, including nested menus.
Example
HTML
<body contextmenu="share">
<menu type="context" id="share">
<menu label="share">
<menuitem label="Twitter" onclick="shareViaTwitter()"></menuitem>
<menuitem label="Facebook" onclick="shareViaFacebook()"></menuitem>
</menu>
</menu>
<ol>
<li>
Anywhere in the example you can share the page on Twitter and
Facebook using the Share menu from your context menu.
</li>
<li contextmenu="changeFont" id="fontSizing">
On this specific list element, you can change the size of the text
by using the "Increase/Decrease font" actions from your context menu
</li>
<menu type="context" id="changeFont">
<menuitem label="Increase Font" onclick="incFont()"></menuitem>
<menuitem label="Decrease Font" onclick="decFont()"></menuitem>
</menu>
<li contextmenu="ChangeImage" id="changeImage">
On the image below, you can fire the "Change Image" action
in your Context Menu.<br />
<img src="https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/promobutton_mdn5.png"
contextmenu="ChangeImage" id="promoButton" />
<menu type="context" id="ChangeImage">
<menuitem label="Change Image" onclick="changeImage()"></menuitem>
</menu>
</li>
</ol>
</body>
JavaScript
function shareViaTwitter() {
window.open("https://twitter.com/intent/tweet?text=" +
"Hurray! I am learning ContextMenu from MDN via Mozilla");
}
function shareViaFacebook() {
window.open("https://facebook.com/sharer/sharer.php?u=" +
"/wiki/en/HTML/Element/Using_HTML_context_menus");
}
function incFont() {
document.getElementById("fontSizing").style.fontSize = "larger";
}
function decFont() {
document.getElementById("fontSizing").style.fontSize = "smaller";
}
function changeImage() {
var index = Math.ceil(Math.random() * 39 + 1);
document.images[0].src =
"https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/promobutton_mdn"%3b%20 %0a%20%20%20%20%20%20index%20 %20"%3b.png";
}
Result
Specifications
Specification | Status | Comment |
---|---|---|
HTML 5.1 The definition of 'contextmenu' in that specification. | Recommendation | Snapshot of HTML Living Standard, initial definition. |
Browser compatibility
BCD tables only load in the browser
[1] An experimental implementation was originally available via the command line option --enable-blink-features=ContextMenu
. Until Chrome 52 and Opera 39 it was additionally available by enabling the Experimental Web Platform features option, but got removed from that due to a Web compatibility issue. In June 2017, it was removed entirely from the browsers. This is documented in Chrome bug 87553.
[2] Support for the contextmenu
attribute has been removed from Firefox Mobile (bug 1424252).
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论