首页 > 解决方案 > 学习 JS - 无法将计数器的结果用于字段

问题描述

我真的很陌生,所以请原谅我的无知。

我正在尝试通过添加错误计数器来改进练习打字速度代码。我设法在 HTML 和 css 上为计数器添加了新字段。但是,我只是没有设法将错误计数器的结果放在这个新创建的字段中。我已经制作了计数器的 console.log,它工作正常。任何帮助表示赞赏。问候。

我尝试了使用 innerHTML 和不同字段的各种变体,但均未成功。

<!--HTML-->
<section class="error-counter-area">
   <h5 id="ectitle">ERROR COUNTER</h5>
   <div class="error-counter">0000</div>
</section>


//JS
const theErrorCounter = document.querySelector(".error-counter");

//var errorCounter = 0000; //Initial 4 digits format for errorCounter, but I don't know how to make it work in the same time with the counter inside the function...

function errorCounterRun() {
   var i = 0;
   errorCounter = i++;
   theErrorCounter.innerHTML = errorCounter;
   console.log(errorCounter); // This works fine, it counts except that it is counting the backspace too, but that is another problem...
}

计数器应显示在 errorCounter H5 字段中,而不仅仅是在 console.log 中。

标签: javascriptfunctioninnerhtml

解决方案


推荐阅读