HTML oncontextmenu 이벤트 속성은 사용자가 HTML 요소를 마우스 오른쪽 버튼으로 클릭하여 HTML 문서에서 컨텍스트 메뉴를 열 때 트리거됩니다.
구문
다음은 구문입니다 -
<tagname oncontextmenu=”script”></tagname>
예시
HTML oncontextmenu 이벤트 Attribute의 예를 살펴보겠습니다. −
<!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 이벤트 속성이 작동하는 방식을 관찰해 보십시오.
