[英]SyntaxError on “function x.y () {…}”?
How can I fix the following javascript error ? 如何解决以下javascript错误? This is a handler for an ASP.NET page to disable postbacks when the enter key is pressed: 这是ASP.NET页的处理程序,可在按下Enter键时禁用回发:
<script type="text/javascript">
function document.onkeydown() {
if (event.keyCode == 13) {
event.returnValue = false;
event.cancel = true;
}
}
</script>
Note that document.onkeydown
is not a valid function name. 请注意, document.onkeydown
不是有效的函数名称。 You probably wanted to do this: 您可能想这样做:
document.onkeydown = function(ev) {
if (ev.keyCode == 13) {
// ...
}
Or better: 或更好:
document.addEventListener('keydown', function(ev) {
if (ev.keyCode == 13) {
// ...
});
To add to maerics answer, to get access to the event, you need it as an argument... 要添加到数学答案中,要访问事件,您需要将其作为参数...
document.addEventListener( 'keydown', function( event ) {
console.log( event, event.keyCode );
if (event.keyCode == 13) {
event.returnValue = false;
event.cancel = true;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.