dp19001 2017-06-19 15:01
浏览 53
已采纳

将javascript数组解析为具有相同类名的相应输入字段

Actually I'm working on a function that compares two dates (one date I get from my hidden input, another date - current datetime). I wrote a function in JS which works perfect:

var elements = document.getElementsByClassName("getTime");

        var names = [];
        for(var i=0; i<elements.length; i++) {
            names = elements[i].value;
            var dece = moment(names).format();
            var cureTime = moment().format();
            seco=moment().diff(dece, 'seconds');
            if ((Math.floor(seco / 60)) >= 1 && (Math.floor(seco / 60))<60) {
                console.log(Math.floor(seco / 60) + " minutes");
                document.getElementsByClassName("time").innerHTML = Math.floor(seco / 60) + " minutes ago";
            }else if(((Math.floor(seco / 3600)) >= 1) && ((Math.floor(seco / 3600))<24))
            {
                console.log(Math.floor(seco / 3600) + " hours");
                document.getElementsByClassName("time").innerHTML = Math.floor(seco / 3600) + " hours ago";
            }else if (((Math.floor(seco / 86400)) >= 1) && ((Math.floor(seco / 86400)) <30))
            {
                console.log(Math.floor(seco / 86400) + " days");
                document.getElementsByClassName("time").innerHTML = Math.floor(seco / 86400) + " days ago";
            }else if (((Math.floor(seco / 2592000)) > 1) && ((Math.floor(seco / 2592000)) <= 12))
            {
                console.log(Math.floor(seco / 2592000) + " months");
                document.getElementsByClassName("time").innerHTML = Math.floor(seco / 2592000) + " months ago";
            }else if((Math.floor(seco / 31536000)) > 1)
            {
                console.log(Math.floor(seco / 31536000) + " years");
                document.getElementsByClassName("time").innerHTML = Math.floor(seco / 31536000) + " years ago";
            }
        }

As a result all of those console.logs (in each if statement) return me the right difference in dates. But now I can't understand how can I parse all of these differences into the appropriate span fields with the same class name. My HTML looks like this:

@foreach($repoData as $kek)
     <span class="time" id="openedOn"></span>
    <input type="hidden" class="getTime" id="time" value='{!! $kek->created_at !!}'>
@endforeach

To be clear this FOREACH generates me several dates that are stored in the hidden input. Then I get values from this hidden input and pass them to my function in JS. So I can't understand how should I parse elements from my JS function to that span with class name - "time". Please help.

  • 写回答

1条回答 默认 最新

  • dqrl3595 2017-06-19 15:25
    关注

    getElementsByClassName functions returns an array(Read more here) and hence you need to use array notation to access its elements

    document.getElementsByClassName("time")[0].innerHTML
    

    In your case, since your accessing the elements within a loop you should simply make use of the counter(i).

    document.getElementsByClassName("time")[i].innerHTML
    

    Replace the above to all the conditions for it to work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘