我有一个工作正常的javascript代码,但我希望在一个页面上有多个时钟。
例如,我需要3个时钟。
我不知道如何在getelementbyid中具有多个ID?

<div id="time">October 25, 2018 18:00 GMT+0300 (CDT)</div>



var timestamp = document.getElementById('time'),
t         = new Date(timestamp.innerHTML),
hours     = t.getHours(),
min       = t.getMinutes() + '',

months    = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

if(min.length == 1) min = '0' + min;
timestamp.innerHTML =  hours + ':' + min ;

最佳答案

这里的每个人都在正确的答案上,我只是认为,如果您看到一些代码来引用他们的言论,那将会有所帮助



var timestamp1 = document.getElementById('time1')
getTime(timestamp1)
var timestamp2 = document.getElementById('time2')
getTime(timestamp2)
var timestamp3 = document.getElementById('time3')
getTime(timestamp3)

    function getTime(obj){
        t = new Date(obj.innerHTML),
        hours = t.getHours(),
        min = t.getMinutes() + '',

        months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

        if(min.length == 1){
             min = '0' + min;
         }

        obj.innerHTML =  hours + ':' + min;
    }

<div id="time1">October 25, 2018 18:00 GMT+0300 (CDT)</div>
<div id="time2">October 25, 2018 18:00 GMT+0300 (CDT)</div>
<div id="time3">October 25, 2018 18:00 GMT+0300 (CDT)</div>





请告诉我这是否有帮助!谢谢!

关于javascript - 如何在一页上有多个时钟?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52987334/

10-11 14:58