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

<!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="#" οnclick="document.getElementById('htmlSource').style.display='block';return false">View HTML</a></li>
	<li><a href="#" οnclick="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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值