简体   繁体   English

将跨度显示更改为只读输入

[英]Changing a span display into a readonly input

I have a timer countdown which displays in a <span> , I need it to output into a readonly <input> instead so I can calculate with the value.我有一个显示在<span>的计时器倒计时,我需要将它输出到只读<input>以便我可以使用该值进行计算。 Is there any way to change this?有什么办法可以改变这种情况吗?

I tried我试过

document.getElementById("timedown").value = display;

I tried various other things which just got messy and didn't work.我尝试了各种其他的东西,但这些东西变得凌乱而没有用。 Below is the function and the input下面是函数和输入

function startTimer(duration, display) {
var start = Date.now(),
    diff,
    minutes,
    seconds;
function timer() {
    diff = duration - (((Date.now() - start) / 1000) | 0);

    minutes = (diff / 60) | 0;
    seconds = (diff % 60) | 0;

    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = minutes + ":" + seconds; 

    if (diff <= 0) {
        // faaopoopo se tasi sekone

        start = Date.now() + 1000;
    }
};
// amata vave
timer();
setInterval(timer, 1000);
}

window.onload = function () {  
var fiveMinutes = 60 * 10,
    display = document.querySelector('#time');

startTimer(fiveMinutes, display);
};
<div class="transtime">
  <form>
    <input type="text" id="timedown" value="0" readonly/>
  </form>
</div>

Here is a working codepen Change the function to this:这是一个有效的 codepen将功能更改为:

    function timer() {
    diff = duration - (((Date.now() - start) / 1000) | 0);

    minutes = (diff / 60) | 0;
   seconds = (diff % 60) | 0;

    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.value= minutes + ":" + seconds; 

    if (diff <= 0) {
        // faaopoopo se tasi sekone

        start = Date.now() + 1000;
    }
};

changing the value instead of the text value and it should work更改值而不是文本值,它应该可以工作

Change改变

display.textContent = minutes + ":" + seconds; 

to

document.getElementById("timedown").value = minutes + ":" + seconds; 

or change或改变

display = document.querySelector('#timedown');

and

display.value = minutes + ":" + seconds; 

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

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