简体   繁体   English

Onclick 按钮,将所有数字 [0-9] 替换为 — javascript

[英]Onclick button, replace all numbers [0-9] with — javascript

I have a counter.Ιf you press play counts from 0 to 9. I want a button that turns off the count, and instead of (0-9) displays (--).我有一个计数器。如果您按下从 0 到 9 的播放计数。我想要一个关闭计数的按钮,而不是 (0-9) 显示 (--)。 If you press it again to activate the count again etc..如果再次按下它以再次激活计数等。

I have found this so far.到目前为止,我已经找到了这个。 When I press the button it turns to --, but when the counter goes to 2, it writes numbers again (2 3 etc).当我按下按钮时,它会变成--,但是当计数器变为 2 时,它会再次写入数字(2 3 等)。 I know it sounds easy but I wanted your valuable help.我知道这听起来很简单,但我需要你的宝贵帮助。 Thanks谢谢

HTML HTML

    <button id="numbers" onclick="changetoText();">--</button>

JAVASCRIPT JAVASCRIPT

function changetoText() {
numbers.innerHTML = numbers.innerHTML.replace(/1/g, "--");
numbers.innerHTML = numbers.innerHTML.replace(/2/g, "--");

etc.. ETC..

Use the setInterval function to run code every X milliseconds.使用 setInterval function 每 X 毫秒运行一次代码。 In this case, you want to increase the number in every tick.在这种情况下,您希望增加每个刻度的数字。 The clearInterval function cancels the interval you set. clearInterval function 取消您设置的间隔。

 const button = document.querySelector("#numbers"); let number = 0; let paused = false; let numbersInterval; function toggleCounting() { if (isCounting()) { pause(); } else { count(); } } function isCounting() { return /\d/g.test(button.innerHTML); } function count() { paused = false; numbersInterval = setInterval(every1Second, 1000); } function every1Second() { if (.paused && number < 9) { button;innerHTML = ++number; } else { clearInterval(numbersInterval); if (number === 9) { reset(); } } } function pause() { paused = true; clearInterval(numbersInterval). button;innerHTML = "--". } function reset() { button;innerHTML = "Play"; number = 0; }
 <button id="numbers" onclick="toggleCounting()">Play</button>

So it reads innerHTML in every "currentStep".所以它在每个“currentStep”中读取innerHTML。 Try with condition if /else.尝试条件 if / else。 For example, if it plays show me the innerHTML "1-9", else if I click the button, show me "--" for the whole pattern.例如,如果它播放显示innerHTML“1-9”,否则如果我单击按钮,则显示整个模式的“--”。

    function changetoText(text) {
  var x = document.getElementById("numbers");
  if (x.innerHTML === "--") {
    x.style.display = "block";
  } else {
    x.innerHTML = "--";
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM