The problem is in the removal: display:none
registered in inline styles, therefore when executing the line
menu.style.display = "";
The default value, in the specific case of block
restored.
When transferred to styles, when this line is executed, nothing changes, and the style will continue to take display:none
.
Therefore, it must be set to a block
value for display.
menu.style.display = "block";
For example:
function defPosition(event) { var x = y = 0; if (document.attachEvent != null) { // Internet Explorer & Opera x = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft); y = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); } else if (!document.attachEvent && document.addEventListener) { // Gecko x = event.clientX + window.scrollX; y = event.clientY + window.scrollY; } else { // Do nothing } return { x: x, y: y }; } function menu(type, evt) { // ΠΠ»ΠΎΠΊΠΈΡΡΠ΅ΠΌ Π²ΡΠΏΠ»ΡΠ²Π°Π½ΠΈΠ΅ ΡΠΎΠ±ΡΡΠΈΡ contextmenu evt = evt || window.event; evt.cancelBubble = true; // ΠΠΎΠΊΠ°Π·ΡΠ²Π°Π΅ΠΌ ΡΠΎΠ±ΡΡΠ²Π΅Π½Π½ΠΎΠ΅ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ½ΠΎΠ΅ ΠΌΠ΅Π½Ρ var menu = document.getElementById("contextMenuId"); var html = ""; switch (type) { case (1): html = "<li>webRefferal.com</li>"; html += "<a href='#'>ΠΠ΅ΡΠ²Π°Ρ ΡΡΠ½ΠΊΡΠΈΡ</a>"; html += "<a href='#'>ΠΡΠΎΡΠ°Ρ ΡΡΠ½ΠΊΡΠΈΡ</a>"; html += "<a href='#'>Π’ΡΠ΅ΡΡΡ ΡΡΠ½ΠΊΡΠΈΡ</a>"; break; } // ΠΡΠ»ΠΈ Π΅ΡΡΡ ΡΡΠΎ ΠΏΠΎΠΊΠ°Π·Π°ΡΡ - ΠΏΠΎΠΊΠ°Π·ΡΠ²Π°Π΅ΠΌ if (html) { menu.innerHTML = html; menu.style.top = defPosition(evt).y + "px"; menu.style.left = defPosition(evt).x + "px"; menu.style.display = "block"; } // ΠΠ»ΠΎΠΊΠΈΡΡΠ΅ΠΌ Π²ΡΠΏΠ»ΡΠ²Π°Π½ΠΈΠ΅ ΡΡΠ°Π½Π΄Π°ΡΡΠ½ΠΎΠ³ΠΎ Π±ΡΠ°ΡΠ·Π΅ΡΠ½ΠΎΠ³ΠΎ ΠΌΠ΅Π½Ρ return false; } // ΠΠ°ΠΊΡΡΠ²Π°Π΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ½ΠΎΠ΅ ΠΏΡΠΈ ΠΊΠ»ΠΈΠΊΠ΅ Π»Π΅Π²ΠΎΠΉ ΠΈΠ»ΠΈ ΠΏΡΠ°Π²ΠΎΠΉ ΠΊΠ½ΠΎΠΏΠΊΠΎΠΉ ΠΏΠΎ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΡ // Π€ΡΠ½ΠΊΡΠΈΡ Π΄Π»Ρ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΎΠ² ΡΠΎΠ±ΡΡΠΈΠΉ function addHandler(object, event, handler, useCapture) { if (object.addEventListener) { object.addEventListener(event, handler, useCapture ? useCapture : false); } else if (object.attachEvent) { object.attachEvent('on' + event, handler); } else alert("Add handler is not supported"); } addHandler(document, "contextmenu", function() { document.getElementById("contextMenuId").style.display = "none"; }); addHandler(document, "click", function() { document.getElementById("contextMenuId").style.display = "none"; });
.block { width: 600px; height: 300px; margin: auto; } .block div:first-child { height: 300px; border: 1px solid red; background-color: transprent; } #contextMenuId a, li { border: 1px solid #fefefe; height: 20px; line-height: 20px; width: 200px; text-align: center; display: block; padding: 4px 3px; } #contextMenuId { width: 200px; position: absolute; top: 0; left: 0; border: 1px solid #666; background-color: #CCC; display: none; float: left; }
<div class="block"> <div oncontextmenu="return menu(1, event)">ΠΠ»ΠΈΠΊΠ½ΠΈ ΠΏΡΠ°Π²ΠΎΠΉ ΠΊΠ½ΠΎΠΏΠΊΠΎΠΉ</div> <div id="contextMenuId" style=""></div> </div>
<div oncontextmenu
with<div oncontextmenu
and<div id="contextMenuId"
? - Grundy