HTML oncontextmenu 事件属性
htmlweb developmentfront end technology更新于 2024/5/20 21:32:17
当用户右键单击 HTML 元素以打开 HTML 文档中的上下文菜单时,会触发 HTML oncontextmenu 事件属性。
语法
以下是语法 −
<tagname oncontextmenu=”script></tagname>
示例
让我们看一个 HTML oncontextmenu 事件属性的示例 −
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #000;
height: 100vh;
background-color: #FBAB7E;
background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%);
text-align: center;
padding: 20px;
}
.box {
background: #db133a;
width: 140px;
height: 140px;
margin: 1rem auto;
color: #fff;
}
</style>
</head>
<body>
<h1>HTML oncontextmenu Event Attribute Demo</h1>
<p oncontextmenu="get()" class="box"></p>
<p>Now try to right click on above red box to open context menu.</p>
<script>
function get() {
document.body.style.background = "#084C61";
document.body.style.color = '#fff';
}
</script>
</body>
</html>
输出

现在尝试右键单击红色框以观察 oncontextmenu 事件属性如何工作。

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

