2017-02-23 120 views

回答

-1

將JS文件here複製爲menu.js,並將其包含在您的html文件路徑中。

menu.css文件

ul.SimpleContextMenu { 
display: none; 
position: absolute; 
margin: 0px; 
padding: 0px; 
font-family: verdana; 
font-size: 12px; 
list-style-type: none; 
border-top: 1px solid #000000; 
border-left: 1px solid #000000; 
border-right: 1px solid #000000; 
}ul.SimpleContextMenu li { 
border-bottom: 1px solid #000000; 
} 
ul.SimpleContextMenu li a { 
display: block; 
//menu width 
width: 200px; 
padding: 2px 10px 3px 10px; 
text-decoration: none; 
//Menu font color 
color: #ff0000; 
//Menu background color 
background: #eeeeee; 
} 
ul.SimpleContextMenu li a:hover { 
text-decoration: none; 
color: #ffffff; 
background: #ff0000; 
} 

HTML文件

<html> 
<head> 
<link type="text/css" rel="stylesheet" href="menu.css" /> 
<script type="text/javascript" src="menu.js"></script> 
<script type="text/javascript"> 
SimpleContextMenu.setup({'preventDefault':true}); //prevent default 
SimpleContextMenu.attach('container1', 'CM1'); 
SimpleContextMenu.attach('container2', 'CM2'); 
</script> 
</head> 
<body> 
<div id="container"><ul id="CM1" class="SimpleContextMenu"> 
<li><a href="https://link_1_here">link 1</a></li> 
<li><a href="https://link_2_here">link 2</a></li> 
<li><a href="https://link_2_here">link 3</a></li> 
</ul> 
<div class="container1" style="border: 1px dashed red; margin-top: 30px; height: 50px; background: #f2f2f2;">Right Click Here To See The Menu</div> 
</div> 
</html> 

這個工程在瀏覽器和Mozilla的罰款。

+0

謝謝。我試過這個代碼,但它也阻止了右鍵單擊。我需要打開右鍵點擊自定義彈出菜單 – user1636102

+0

這裏有一個教程,其中介紹瞭如何構建自定義JavaScript上下文菜單的一步一步的介紹。看看這是否有幫助https://www.sitepoint.com/building-custom-right-click-context-menu-javascript/ – Arun

+0

我已經完成了使用SWT。但我需要防止默認的彈出窗口。任何想法?? – user1636102

1

請嘗試:

$(document).ready(function() 
{ 
    $(document).bind("contextmenu",function(e){ 
     return false; 
    }); 
}); 

其實,這很煩人,當你不能在網站上使用鼠標右鍵。

您可以禁用它的任何特定塊以同樣的方式:

$(document).ready(function() 
{ 
    $('#test').bind("contextmenu",function(e){ 
     return false; 
    }); 
});​ 

樣本HTML:

<div id="test"> ​No right clicks allowed here! </div>​ 

而且樣品CSS:

#test { width: 200px; height: 200px; background: red; } 

最後,一個示例