日期:2014-05-16  浏览次数:20358 次

怎样用js检测选中文字
选中了文字,然后按下“alt”按键,然后执行js 弹出该文字,js或jquery应该怎么写?

------解决方案--------------------

<!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=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<title>无标题文档</title>
</head>

<body>

<div id="content">this is demo:pktandy@apobates.com</div>

<script type="text/javascript">
jQuery(function($){
$('#content').mouseup(function(e){     
   var userSelection, text; 
   if (window.getSelection) { 
         userSelection = window.getSelection(); 
} else if (document.selection) { 
    userSelection = document.selection.createRange(); 
   } 
   try { 
    text = userSelection.toString(); 
   } catch (err) { 
    text = userSelection.text; 
   } 
alert(text);
});
});
</script>
</body>
</html>