JS 屏蔽右键,创建右键菜单
来源:互联网 发布:mac系统如何剪切文件 编辑:程序博客网 时间:2024/06/06 09:22
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style type="text/css">
body{
font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif;
margin-left:0px;
}
#contextMenu{ /* The menu container */
border:1px solid #202867; /* Border around the entire menu */
background-color:#FFF; /* White background color of the menu */
margin:0px;
padding:0px;
width:175px; /* Width of context menu */
font-family:arial;
font-size:12px;
background-image:url('images/gradient.gif');
background-repeat:repeat-y;
/* Never change these two values */
display:none;
position:absolute;
}
#contextMenu a{ /* Links in the context menu */
color: #000;
text-decoration:none;
line-height:25px;
vertical-align:middle;
height:28px;
/* Don't change these 3 values */
display:block;
width:100%;
clear:both;
}
#contextMenu li{ /* Each menu item */
list-style-type:none;
padding:1px;
margin:1px;
cursor:pointer;
clear:both;
}
#contextMenu li div{ /* Dynamically created divs */
cursor:pointer;
}
#contextMenu .contextMenuHighlighted{ /* Highlighted context menu item */
border:1px solid #000;
padding:0px;
background-color:#E2EBED;
}
#contextMenu img{
border:0px;
}
#contextMenu .imageBox{ /* Dynamically created divs for images in the menu */
float:left;
padding-left:2px;
padding-top:3px;
vertical-align:middle;
width: 30px; /* IE 5.x */
width/* */:/**/28px; /* Other browsers */
width: /**/28px;
}
#contextMenu .itemTxt{
float:left;
width: 120px; /* IE 5.x */
width/* */:/**/140px; /* Other browsers */
width: /**/140px;
}
</style>
<script type="text/javascript">
var contextMenuObj;
var MSIE = navigator.userAgent.indexOf('MSIE')?true:false;
var navigatorVersion = navigator.appVersion.replace(/.*?MSIE (\d\.\d).*/g,'$1')/1;
var activeContextMenuItem = false;
function highlightContextMenuItem()
{
this.className='contextMenuHighlighted';
}
function deHighlightContextMenuItem()
{
this.className='';
}
function showContextMenu(e)
{
contextMenuSource = this;
if(activeContextMenuItem)activeContextMenuItem.className='';
if(document.all)e = event;
var xPos = e.clientX;
if(xPos + contextMenuObj.offsetWidth > (document.documentElement.offsetWidth-20)){
xPos = xPos + (document.documentElement.offsetWidth - (xPos + contextMenuObj.offsetWidth)) - 20;
}
var yPos = e.clientY;
if(window.document.body.scrollTop > 0)
{
yPos = (window.screen.Height) ? e.clientY + window.document.body.scrollTop -20 : e.clientY -20;
}
else if (window.pageYOffset)
{
yPos = (window.pageYOffset > 0) ? e.clientY + window.pageYOffset -20 : e.clientY -20;
}
else
{ yPos = e.clientY -20; }
/* * */
contextMenuObj.style.left = xPos + 'px';
contextMenuObj.style.top = yPos + 'px';
contextMenuObj.style.display='block';
return false;
}
function hideContextMenu(e)
{
if(document.all) e = event;
if(e.button==0 && !MSIE){
}else{
contextMenuObj.style.display='none';
}
}
function initContextMenu()
{
contextMenuObj = document.getElementById('contextMenu');
contextMenuObj.style.display = 'block';
var menuItems = contextMenuObj.getElementsByTagName('LI');
for(var no=0;no<menuItems.length;no++){
menuItems[no].onmouseover = highlightContextMenuItem;
menuItems[no].onmouseout = deHighlightContextMenuItem;
var aTag = menuItems[no].getElementsByTagName('A')[0];
var img = menuItems[no].getElementsByTagName('IMG')[0];
if(img){
var div = document.createElement('DIV');
div.className = 'imageBox';
div.appendChild(img);
if(MSIE && navigatorVersion<6){
aTag.style.paddingLeft = '0px';
}
var divTxt = document.createElement('DIV');
divTxt.className='itemTxt';
divTxt.innerHTML = aTag.innerHTML;
aTag.innerHTML = '';
aTag.appendChild(div);
aTag.appendChild(divTxt);
if(MSIE && navigatorVersion<6){
div.style.position = 'absolute';
div.style.left = '2px';
divTxt.style.paddingLeft = '15px';
}
if(!document.all){
var clearDiv = document.createElement('DIV');
clearDiv.style.clear = 'both';
aTag.appendChild(clearDiv);
}
}else{
if(MSIE && navigatorVersion<6){
aTag.style.paddingLeft = '15px';
aTag.style.width = (aTag.offsetWidth - 30) + 'px';
}else{
aTag.style.paddingLeft = '30px';
aTag.style.width = (aTag.offsetWidth - 60) + 'px';
}
}
}
contextMenuObj.style.display = 'none';
document.documentElement.oncontextmenu = showContextMenu;
document.documentElement.onclick = hideContextMenu;
}
</script>
</head>
<body>
<div align="center">点击鼠标右键看效果</center>
<ul id="contextMenu">
<li><a href="http://www.sharejs.com">shareJs</a></li>
<li><a href="http://www.baidu.com">百度一下</a></li>
<li><a href="http://www.souhu.com">搜狐虎</a></li>
<li><a href="http://www.alibaba.com">阿里爸爸</a></li>
</ul>
<script type="text/javascript">
initContextMenu();
</script>
</body>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style type="text/css">
body{
font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif;
margin-left:0px;
}
#contextMenu{ /* The menu container */
border:1px solid #202867; /* Border around the entire menu */
background-color:#FFF; /* White background color of the menu */
margin:0px;
padding:0px;
width:175px; /* Width of context menu */
font-family:arial;
font-size:12px;
background-image:url('images/gradient.gif');
background-repeat:repeat-y;
/* Never change these two values */
display:none;
position:absolute;
}
#contextMenu a{ /* Links in the context menu */
color: #000;
text-decoration:none;
line-height:25px;
vertical-align:middle;
height:28px;
/* Don't change these 3 values */
display:block;
width:100%;
clear:both;
}
#contextMenu li{ /* Each menu item */
list-style-type:none;
padding:1px;
margin:1px;
cursor:pointer;
clear:both;
}
#contextMenu li div{ /* Dynamically created divs */
cursor:pointer;
}
#contextMenu .contextMenuHighlighted{ /* Highlighted context menu item */
border:1px solid #000;
padding:0px;
background-color:#E2EBED;
}
#contextMenu img{
border:0px;
}
#contextMenu .imageBox{ /* Dynamically created divs for images in the menu */
float:left;
padding-left:2px;
padding-top:3px;
vertical-align:middle;
width: 30px; /* IE 5.x */
width/* */:/**/28px; /* Other browsers */
width: /**/28px;
}
#contextMenu .itemTxt{
float:left;
width: 120px; /* IE 5.x */
width/* */:/**/140px; /* Other browsers */
width: /**/140px;
}
</style>
<script type="text/javascript">
var contextMenuObj;
var MSIE = navigator.userAgent.indexOf('MSIE')?true:false;
var navigatorVersion = navigator.appVersion.replace(/.*?MSIE (\d\.\d).*/g,'$1')/1;
var activeContextMenuItem = false;
function highlightContextMenuItem()
{
this.className='contextMenuHighlighted';
}
function deHighlightContextMenuItem()
{
this.className='';
}
function showContextMenu(e)
{
contextMenuSource = this;
if(activeContextMenuItem)activeContextMenuItem.className='';
if(document.all)e = event;
var xPos = e.clientX;
if(xPos + contextMenuObj.offsetWidth > (document.documentElement.offsetWidth-20)){
xPos = xPos + (document.documentElement.offsetWidth - (xPos + contextMenuObj.offsetWidth)) - 20;
}
var yPos = e.clientY;
if(window.document.body.scrollTop > 0)
{
yPos = (window.screen.Height) ? e.clientY + window.document.body.scrollTop -20 : e.clientY -20;
}
else if (window.pageYOffset)
{
yPos = (window.pageYOffset > 0) ? e.clientY + window.pageYOffset -20 : e.clientY -20;
}
else
{ yPos = e.clientY -20; }
/* * */
contextMenuObj.style.left = xPos + 'px';
contextMenuObj.style.top = yPos + 'px';
contextMenuObj.style.display='block';
return false;
}
function hideContextMenu(e)
{
if(document.all) e = event;
if(e.button==0 && !MSIE){
}else{
contextMenuObj.style.display='none';
}
}
function initContextMenu()
{
contextMenuObj = document.getElementById('contextMenu');
contextMenuObj.style.display = 'block';
var menuItems = contextMenuObj.getElementsByTagName('LI');
for(var no=0;no<menuItems.length;no++){
menuItems[no].onmouseover = highlightContextMenuItem;
menuItems[no].onmouseout = deHighlightContextMenuItem;
var aTag = menuItems[no].getElementsByTagName('A')[0];
var img = menuItems[no].getElementsByTagName('IMG')[0];
if(img){
var div = document.createElement('DIV');
div.className = 'imageBox';
div.appendChild(img);
if(MSIE && navigatorVersion<6){
aTag.style.paddingLeft = '0px';
}
var divTxt = document.createElement('DIV');
divTxt.className='itemTxt';
divTxt.innerHTML = aTag.innerHTML;
aTag.innerHTML = '';
aTag.appendChild(div);
aTag.appendChild(divTxt);
if(MSIE && navigatorVersion<6){
div.style.position = 'absolute';
div.style.left = '2px';
divTxt.style.paddingLeft = '15px';
}
if(!document.all){
var clearDiv = document.createElement('DIV');
clearDiv.style.clear = 'both';
aTag.appendChild(clearDiv);
}
}else{
if(MSIE && navigatorVersion<6){
aTag.style.paddingLeft = '15px';
aTag.style.width = (aTag.offsetWidth - 30) + 'px';
}else{
aTag.style.paddingLeft = '30px';
aTag.style.width = (aTag.offsetWidth - 60) + 'px';
}
}
}
contextMenuObj.style.display = 'none';
document.documentElement.oncontextmenu = showContextMenu;
document.documentElement.onclick = hideContextMenu;
}
</script>
</head>
<body>
<div align="center">点击鼠标右键看效果</center>
<ul id="contextMenu">
<li><a href="http://www.sharejs.com">shareJs</a></li>
<li><a href="http://www.baidu.com">百度一下</a></li>
<li><a href="http://www.souhu.com">搜狐虎</a></li>
<li><a href="http://www.alibaba.com">阿里爸爸</a></li>
</ul>
<script type="text/javascript">
initContextMenu();
</script>
</body>
</html>
分两步走:一设置好右键菜单的大小,判断是什么浏览器;二:控制右键时候,将值放入到菜单中去
- JS 屏蔽右键,创建右键菜单
- JS屏蔽右键菜单
- JS 屏蔽右键菜单
- JS 屏蔽右键菜单
- JS 屏蔽右键菜单 .
- js屏蔽右键菜单
- 屏蔽右键菜单+右键事件 js
- 屏蔽右键菜单+右键事件 js
- js屏蔽右键菜单代码
- js屏蔽鼠标右键菜单
- js 屏蔽浏览器右键菜单
- js屏蔽浏览器右键菜单
- 屏蔽右键菜单(JS)
- js屏蔽鼠标右键菜单
- JS 右键创建菜单
- JS屏蔽Cognos报表右键菜单
- 屏蔽右键菜单查看源代码的JS
- 屏蔽右键菜单
- 使用字节码asm将pojo转换成Map,直接构造mongodb bsonobject
- 第八周作业---求函数的值
- C语言学习笔记——scanf("%c",&ch) 读取字符
- include <stdio.h>
- 马士兵spring教程学习总结
- JS 屏蔽右键,创建右键菜单
- Django Session处理
- CountDownLatch and CyclicBarrier
- C# Hadoop学习笔记(六)—C#的云计算框架借鉴(上)
- 第二章 CCNode基类
- java日期相加减
- window 下远程登录linux
- C++ 操作符优先级
- HBASE shell 常用命令大全