function getSelectText(editor) {
if (!editor) return editor.focus()
if (editor.document && editor.document.selection)
return editor.document.selection.createRange().text
else if ("selectionStart" in editor)
return editor.value.substring(editor.selectionStart, editor.selectionEnd)
}
//调用方式
var text= getSelectText(document.getElementById("txtName"))
<!DOCTYPE html><html>
<head>
<title></title>
</head>
<body>
<div>文本框:</div>
<textarea>按下ESC选中我</textarea>
<script src="jquery.min.js" ></script>
<script type="text/javascript">
function hotkey()
{
var a=window.event.keyCode
if(a==27)
{
$('textarea').select()
}
}
// 初始化加载
$(document).ready(function () {
document.onkeydown = hotkey
})
</script>
</body>
</html>