JS 如何实现按下Esc按键全选文本框中的文本内容?

JavaScript010

JS 如何实现按下Esc按键全选文本框中的文本内容?,第1张

<!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>

显示层:

css:

.showdiv

{

width: 100%

height: auto

position: absolute

left: 0

top: 0

z-index: 999

display: none

}

.count_div

{

width: 500px

height: 400px

margin-top: 120px

margin-left: auto

margin-right: auto

margin-bottom: 0

border: 1px solid #aaaaaa

background: #fff

}

.brg

{

width: 100%

background: #ededed

position: absolute

top: 0

left: 0

filter: alpha(opacity=60)

-moz-opacity: 0.6

opacity: 0.6

position: absolute

top: 0

left: 0

display: none

z-index: 998

}

js:

function close () {

$("#brg").css("display", "none")

$("#showdiv").css("display", "none")

}

function show() {

$("#brg").css("display", "none")

$("#div_show").css("display", "none")

}

 $(document).bind('keydown', 'esc',function (evt){

//关闭层代码

close ()

return false})

html:

<!--遮罩层-->

<div class="brg" id="brg">

</div>

<!--显示层-->

<div class="showdiv" id="div_order">

<div class="count_div" id="div_order_count">

内容

</div>

</div>

在网上找一个“jquery.hotkeys.js”的js包,里面都是jquery整理好的热键,

引入jquery包。

$(document).bind('keydown', 'esc',function (evt){

//关闭层代码

return false})

手敲,未测试,应该不会有太大问题,根据自己内容修改