javascript keypress function
i thought i had solved this but my basic example seems to not be working. not sure why but i am sure it is a small issue. any help is appreciated.
<html>
<head>
</head&开发者_开发技巧gt;
<body>
<textarea rows="20" cols="61" id="entry" name="entry" class="body_text" wrap="physical">
</textarea>
</body>
</html>
<script>
function keypress(e){
var key_s = (window.event) ? event.keyCode : e.keyCode;
document.getElementById("entry").innerHTML=key_s;
}
</script>
You should add the keypress event listener to the browser. That can be done in several ways:
- rename keypress to onkeypress (-> window.onkeypress)
use addEventListener or attachEvent (IE specific):
if(window.addEventListener) window.addEventListener('keypress', keypress, false); else if(window.attachEvent) window.attachEvent('onkeypress', keypress); else window.onkeypress = keypress;
What's happening? First, check if the standard way to handle events is available. If so, use it. Otherwise, use Internet Explorers method. If you're really using an old browser, use a legacy method.
You should also use 'value' instead of 'innerHTML' on textarea's.
精彩评论