我想统计用户点击每次鼠标点击的次数,所以我创建了一个函数,但它只计算第一次点击。我创建一个变量来存储计数值。点击计数功能不起作用?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>clicks</title>
<script type="text/javascript">
function clickcount(){
var clicks=0;
document.getElementById('count').value= ++clicks;
}
document.onclick=clickcount;
</script>
</head>
<body>
<input type="text" id="count" />
</body>
</html>
http://jsfiddle.net/vuASn/ – xandercoded 2012-02-16 03:29:19
现在我明白了,如果我把varibale出来的函数,那么increaments它的价值。现在我的问题是为什么我们需要把这个变量放在函数 – 2012-02-16 03:32:09