`

鼠标右键选中copy功能(IE&FF兼容)

    博客分类:
  • js
阅读更多
<!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" />
<title>鼠标右键菜单 </title>
	<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">
	
	/************************************************************************************************************
	(C) www.dhtmlgoodies.com, October 2005
	
	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
	
	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.
	
	Thank you!
	
	www.dhtmlgoodies.com
	Alf Magne Kalleland
	
	************************************************************************************************************/

	var myvalue;
	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;
	}
	
	  function SelectText()
        {
          try{
            var selecter=window.getSelection();
            if(selecter!=null&&selecter.trim()!=""){
            myvalue=selecter;

              }
          }catch(err){
        
            var selecter;
            var s;
            
            if(navigator.userAgent.indexOf("MSIE")>0){
             selecter=document.selection.createRange();
              myvalue=selecter.text;
              
            }else if(navigator.userAgent.indexOf("Firefox")>0){
            selecter=window.getSelection().getRangeAt(0);
            myvalue=selecter;
            
            }
          }
        }
       function CopyToText(){
       document.getElementById('txt').value=myvalue; 
       //document.all.txt.value=myvalue;
       } 
        String.prototype.trim=function()
        {
          return this.replace(/(^\s*)|(\s*$)/g,"");
        }

	</script>

</head>
<body>
<ul id="contextMenu">
	<li><a href="http://www.sharejs.com"><img src="images/button.gif">Home</a></li>
	<li><a href="#" onclick="document.getElementById('htmlSource').style.display='block';return false">View HTML</a></li>
	<li><a href="#" onclick="CopyToText()">拷贝到文本区</a></li>
	<li><a href="#">Menu item 4</a></li>
</ul>
<input   type="text"   size=60   name="txt" id="txt"> 
<br>
<script type="text/javascript">
initContextMenu();
</script>

<span onClick="SelectText()">河中鱼类离奇死亡,下游居民频染怪病,沿岸植物不断变异,是残留农药?还是生化攻击?敬请关注今晚CCTV-10《科学探索》,即将播出的专题节目:《神秘的河边洗脚人--中国男足》</span>

</body>
</html>

 

分享到:
评论

相关推荐

    C#WPF 右键菜单 显示 事件触发 测试通过

    C#WPF 右键菜单 显示 事件触发 测试通过 &lt;!-- 设置右键菜单 --&gt; 编辑"&gt; &lt;!-- 调用系统命令 --&gt; &lt;MenuItem Command="Copy"&gt; &lt;MenuItem Command="Cut"&gt;&lt;/MenuItem&gt; &lt;MenuItem Command="Paste"&gt;&lt;/MenuItem&gt; ...

    CxGrid右键增强功能源代码

    //右键菜单 CopyCell:TMenuItem;//复制单元格内容 CopyList:TMenuItem;//复制选中列内容 CopyLine:TMenuItem;//复制选中行内容 PastLine:TMenuItem;//粘贴#9分隔符数据 DerFiles:TMenuItem;//导出文件 ...

    Windows右键添加“在此处打开CMD”

    程序员使用cmd的时候还是比较多的,本工具实现了在任何目录下右键cmd,并自动cd到目录,非常好用,有需要的小伙伴可以试试。 使用,双击加入注册表就行,原理很简单,就是使用了Powershell和一些cd命令。

    FF--IE (兼容) 数据直接添加到剪切板

    FF--IE (兼容) 数据直接添加到剪切板 function CopyContent(copy) { if (window.clipboardData) { window.clipboardData.setData("Text", copy); } else if (window.netscape) { netscape.security....

    Chrome插件Enable Copy破解网站禁止复制插件

    1.Enable Copy 插件可以破解掉网页中因网站开发者使用JavaScript脚本启动的网页禁止复制功能,如果是别的方式(比如网页上的文本使用flash插件载入来禁止用户复制、右键等功能)的时候,Enable Copy 插件的破解方法...

    快速复制软件FastCopy3.05.23官方中文版.rar

    便捷的拖曳式操作,可以拖放一个或多个文件到来源,支持整合到鼠标右键菜单,通过右键直接快速复制,内置三种复制模式,支持不覆盖差分复制,按大小/日期差分复制,按最新日期差分复制,可调节缓存大小,支持文件...

    QCopy网络文件复制v1.0.8绿色版

    Q-Copy是一个附加于系统的工具,只需对任何目录或文件按鼠标右键即会出现Q-Copy这个功能,这个功能可让你做拷贝、移动、分割文件等功能。它提供你更容易、更快速的方法拷贝和移动文件,而分割文件功能则可让你将较大...

    js禁止右键 禁止复制

    js禁止右键 禁止复制代码 js禁止右键 禁止复制代码 js禁止右键 禁止复制代码

    DeepCopy-ShallowCopy:DeepCopy&ShallowCopy

    DeepCopy-ShallowCopy:DeepCopy&ShallowCopy

    FastCopy 快速复制粘贴

    只使用鼠标右键,就能快速,方便的复制粘贴,而不需要点击鼠标右键。再选择复制或粘贴。

    用Copy Handler复制文件

    使用鼠标右键拖拽一些文件/夹到某个地方,或者右键单击它们时,在环境菜单中都会出现使用(CH)进行操作的菜单。 复制某些文件到剪贴板 (在资源管理器中选择一些文件,使用键盘按ctrl+C或在资源管理器环境菜单中选用...

    datagrid copy & paste

    datagrid copy & paste

    FastCopy_CN

    FastCopy 主要包含下列功能: 1.完全支持拖曳操作,支持拖曳多个文件到来源中; 2.支持外壳整合,方便的让你利用右键菜单直接复制文件; 3.支持三种不同的 HDD 模式; 4.内建多种人性化的操作模式; 5.支持过滤,...

    CopyPath

    一个很简单的插件,安装后,鼠标右键多出来一个Copy Full Path,可以复制文件或目录的全名。很方便吧。

    工控图库软件,ifix

    运行SYMFAC1.exe,选中所需要的图形,鼠标右键,COPY 2.打开IFIX软件,在工作台的画图界面,鼠标右键,粘贴。 所需要的图面就能在IFIX画面上显示出来。 另外,这个软件也可以放在别的支持矢量图形软件上进行粘贴...

    10 numpy的 copy & deep copy (教学教程)

    10_numpy的_copy_&_deep_copy_(教学教程)

    android微信支付接口demo

    android微信支付接口demo 微信支付是大家常遇到的功能模块。希望对大家有所帮助!!!

    FastCopy3.09快速复制软件32/64位中文版.rar

    软件介绍: FastCopy是一款应用于32-64位WINDOWS系统的文件快速复制工具,复制小文件时...而且安装后能直接集成到鼠标右键菜单中,方便你使用它来快捷进行文件复制。支持不同HDD模式,支持使用通配符及命令行操作。

    CopyPath-添加“复制文件路径”到右键菜单的小软件

    CopyPath-添加“复制文件路径”到右键菜单的小软件 将压缩包内的copypath.dll解压到c:\windows\system32\  运行命令:regsvr32.exe copypath.dll,安装完成  Copy Path支持以下六种形式的路径复制:  1. "C:\...

Global site tag (gtag.js) - Google Analytics