Styles need to be rendered separately for:

<div oncontextmenu="return menu(1, event)"; style="height:300px; border:1px solid red; background-color:transprent;"> Кликни ΠΏΡ€Π°Π²ΠΎΠΉ ΠΊΠ½ΠΎΠΏΠΊΠΎΠΉ </div> <div id="contextMenuId" style="width:200px; position:absolute; top:0; left:0; border:1px solid #666; background-color:#CCC; display:none; float:left;"> 

ie from inline-style to make at least built-in? the code just stops working

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .block{ width:600px; height:300px; margin:auto; } #contextMenuId a,li{ border:1px solid #fefefe; height:20px; line-height: 20px; width:200px text-align: center; display:block; padding:4px 3px; } </style> </head> <body> <div class="block"> <div oncontextmenu="return menu(1, event)"; style="height:300px; border:1px solid red; background-color:transprent;"> Кликни ΠΏΡ€Π°Π²ΠΎΠΉ ΠΊΠ½ΠΎΠΏΠΊΠΎΠΉ </div> <div id="contextMenuId" style="width:200px; position:absolute; top:0; left:0; border:1px solid #666; background-color:#CCC; display:none; float:left;"></div> </div> <script src="https://geyanpeaple.imtqy.com/js.js"></script> </body> </html> 

The script blocks the ПКМ and opens its context menu.

  • I did not understand: what styles where are they going from? - Grundy
  • at the very top there are inline styles right in the block, I stand them separately in the style tags but js stupidly stops working, for some reason - user33274
  • do you mean two <div oncontextmenu with <div oncontextmenu and <div id="contextMenuId" ? - Grundy
  • Yes ! those inside .block - user33274
  • one
    not really the code stops working, but the div is still invisible - Grundy

1 answer 1

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> 

  • @Geyan, uh, did we ever argue? if you remove js, then the snippet will become inoperative - Grundy
  • one
    @Geyan, no, in this case it is not correct. The script is an integral part of the example, and the answer is to change the script. Therefore, the script text should be in the question and in the answer. - Grundy
  • Hi, hrenova, lan ATP - user33274
  • @Geyan, and what's in it that you need a link? Moreover, the link to your github is in your profile - Grundy
  • so that’s why I wanted to hide it, but in principle, I don’t want to make a problem with this, thanks a lot, was the problem in general due to a conflict in js? - user33274