HTML oncontextmenu 事件屬性


當用戶右鍵單擊 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 事件屬性如何工作。

更新時間:2021 年 2 月 16 日

111 人次瀏覽

開啟 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.