首页javascriptmouseJavascript Event - 如何计数点击和输出点击计数

Javascript Event - 如何计数点击和输出点击计数

我们想知道如何计数点击和输出点击计数。

<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){
(function() {
    var oCount = document.getElementById('count');
    var oResult = document.getElementById('result');
    var nCount = 0;
    oCount.addEventListener('click', function(e) {
        e.preventDefault();
        nCount += 1;
        switch(nCount) {
            case 1:
                oResult.innerHTML = 'One';
                break;
            case 2:
                oResult.innerHTML = 'Two';
                break;
            default:
                oResult.innerHTML = 'More than 2';
        }
    }, false);
})();
}
</script>
</head>
<body>
  <a href="#" id="count">Count!</a>
  <span id="result"></span>
</body>
</html>