Javascript简单timeago脚本设置为跨类或标签ID

时间:2021-09-09 21:31:19

I have a script that originally taken from * question thread. This script will allow the user to notice when was the last time the page has been modified. I want the script to be set by its SPAN CLASS or LABEL ID not by its DIV SPAN. Any help would be much appreciated. Thank you. :)

我有一个最初取自*问题线程的脚本。此脚本将允许用户注意最后一次修改页面的时间。我希望脚本由其SPAN CLASS或LABEL ID设置,而不是由其DIV SPAN设置。任何帮助将非常感激。谢谢。 :)

Here is what I have so far :

这是我到目前为止:

HTML

This page is updated ago

此页面已更新

JavaScript

var date = new Date();
var finaltime = timeSince(date);
setInterval(function(){
  document.getElementsByTagName('span')[0].innerHTML = timeSince(date);
},5000);
document.getElementsByTagName('span')[0].innerHTML = timeSince(date);

function timeSince(date) {
  var seconds = Math.floor((new Date() - date) / 1000);
  var interval = Math.floor(seconds / 31536000);

  if (interval > 1) {
    return interval + " years";
  }
  interval = Math.floor(seconds / 2592000);
  if (interval > 1) {
    return interval + " months";
  }
  interval = Math.floor(seconds / 86400);
  if (interval > 1) {
    return interval + " days";
  }
  interval = Math.floor(seconds / 3600);
  if (interval > 1) {
    return interval + " hours";
  }
  interval = Math.floor(seconds / 120);
  if (interval >= 2){
    return interval + "minutes";
  }
  interval = Math.floor(seconds / 60);
  if (interval == 1) {
    return interval + " minute";
  }

  return "a while";

}

3 个解决方案

#1


0  

Are you looking for something like this?

你在找这样的东西吗?

<div id="samplediv"> This page is updated <r id ="puthere"> </r></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://timeago.yarp.com/jquery.timeago.js"></script>
<script>
   var date = new Date();
   setInterval(function(){
      document.getElementById('samplediv').getElementsByTagName('r')[0].innerHTML = $.timeago(date);
		},500);
   document.getElementById('samplediv').getElementsByTagName('r')[0].innerHTML = $.timeago(date);
</script>

#2


0  

I didn't see your code before, so it's the solution to your current code.

我之前没有看到您的代码,因此它是您当前代码的解决方案。

var pa = document.getElementById('lbl_timeago').parentNode;
        setInterval(function(){
            pa.innerHTML = timeSince(date);
            },5000);
        pa.innerHTML = timeSince(date);

Didn't change much. I've tested, it should work.

没有太大变化。我测试过,它应该可以工作。

In jQuery

$('#puthere').parent();

#3


0  

I have solved this problem by this code.

我通过这段代码解决了这个问题。

HTML

 This page is updated <span class="timeago_span"> </span> ago

JavaScript

var date = new Date();
var finaltime = timeSince(date);
setInterval(function(){
  document.getElementsByClassName('timeago_span')[0].innerHTML = timeSince(date);
},5000);
document.getElementsByClassName('timeago_span')[0].innerHTML = timeSince(date);


function timeSince(date) {
  var seconds = Math.floor((new Date() - date) / 1000);
  var interval = Math.floor(seconds / 31536000);

  if (interval > 1) {
    return interval + " years";
  }
  interval = Math.floor(seconds / 2592000);
  if (interval > 1) {
    return interval + " months";
  }
  interval = Math.floor(seconds / 86400);
  if (interval > 1) {
    return interval + " days";
  }
  interval = Math.floor(seconds / 3600);
  if (interval > 1) {
    return interval + " hours";
  }
  interval = Math.floor(seconds / 120);
  if (interval >= 2){
    return interval + " minutes";
  }
  interval = Math.floor(seconds / 60);
  if (interval == 1) {
    return interval + " minute";
  }

  // return Math.floor(seconds) + " seconds";
  return  " a while "

}

#1


0  

Are you looking for something like this?

你在找这样的东西吗?

<div id="samplediv"> This page is updated <r id ="puthere"> </r></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://timeago.yarp.com/jquery.timeago.js"></script>
<script>
   var date = new Date();
   setInterval(function(){
      document.getElementById('samplediv').getElementsByTagName('r')[0].innerHTML = $.timeago(date);
		},500);
   document.getElementById('samplediv').getElementsByTagName('r')[0].innerHTML = $.timeago(date);
</script>

#2


0  

I didn't see your code before, so it's the solution to your current code.

我之前没有看到您的代码,因此它是您当前代码的解决方案。

var pa = document.getElementById('lbl_timeago').parentNode;
        setInterval(function(){
            pa.innerHTML = timeSince(date);
            },5000);
        pa.innerHTML = timeSince(date);

Didn't change much. I've tested, it should work.

没有太大变化。我测试过,它应该可以工作。

In jQuery

$('#puthere').parent();

#3


0  

I have solved this problem by this code.

我通过这段代码解决了这个问题。

HTML

 This page is updated <span class="timeago_span"> </span> ago

JavaScript

var date = new Date();
var finaltime = timeSince(date);
setInterval(function(){
  document.getElementsByClassName('timeago_span')[0].innerHTML = timeSince(date);
},5000);
document.getElementsByClassName('timeago_span')[0].innerHTML = timeSince(date);


function timeSince(date) {
  var seconds = Math.floor((new Date() - date) / 1000);
  var interval = Math.floor(seconds / 31536000);

  if (interval > 1) {
    return interval + " years";
  }
  interval = Math.floor(seconds / 2592000);
  if (interval > 1) {
    return interval + " months";
  }
  interval = Math.floor(seconds / 86400);
  if (interval > 1) {
    return interval + " days";
  }
  interval = Math.floor(seconds / 3600);
  if (interval > 1) {
    return interval + " hours";
  }
  interval = Math.floor(seconds / 120);
  if (interval >= 2){
    return interval + " minutes";
  }
  interval = Math.floor(seconds / 60);
  if (interval == 1) {
    return interval + " minute";
  }

  // return Math.floor(seconds) + " seconds";
  return  " a while "

}