แอตทริบิวต์เหตุการณ์ oncontextmenu ใน HTML ถูกกระตุ้นเมื่อผู้ใช้คลิกขวาที่องค์ประกอบ HTML เพื่อเปิดเมนูบริบท (context menu) ในเอกสาร HTML
ไวยากรณ์
<tagname oncontextmenu="script"></tagname>
ตัวอย่าง
<!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>ตัวอย่างแอตทริบิวต์เหตุการณ์ oncontextmenu ของ HTML</h1>
<p oncontextmenu="get()" class="box"></p>
<p>ลองคลิกขวาที่กล่องสีแดงด้านบนเพื่อเปิดเมนูบริบท</p>
<script>
function get() {
document.body.style.background = "#084C61";
document.body.style.color = '#fff';
}
</script>
</body>
</html>
ผลลัพธ์
ลองคลิกขวาที่กล่องสีแดงเพื่อดูการทำงานของ oncontextmenu
พื้นหลังจะเปลี่ยนเป็นสีเขียวดำและตัวอักษรเป็นสีขาวเมื่อคลิกขวา