[英]javascript - Uncaught SyntaxError: Invalid or unexpected token
I AM trying to catch all document click events, but i get error: 我正在尝试捕获所有文档单击事件,但出现错误:
javascript - Uncaught SyntaxError: Invalid or unexpected token
this is the code: 这是代码:
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script>
document.getElementsByTagName('body').onclick = function(e){
var i=0;//for example
}// error over here
</script>
</body>
</html>
Thanks 谢谢
You have an invalid character here, directly after the curly bracket. 您在大括号后的此处有一个无效字符。
}// error over here
// ^
Beside the problem of using the wrong object. 除了使用错误对象的问题。 You better address it with
你最好用
document.body.onclick = ...
document.getElementsByTagName('body').onclick
shold be be
document.getElementsByTagName('body')[0].onclick
because getElementsByTagName
will return an HTML Collection 因为
getElementsByTagName
将返回HTML集合
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.