简体   繁体   English

一段时间后如何更改文本的颜色?

[英]How to Change color of a text after a time period?

I have created a notice board. 我已经创建了一个公告板。 I want to show the notices in Red color for the first 48 hours. 我想在前48小时内以红色显示通知。 After 48 hour the color of the Notices will be changed. 48小时后,通知的颜色将更改。 Now what should i change or add in my code? 现在我应该更改或添加什么代码?

HTML body for the notice board - 公告栏的HTML正文-

<div class="col-md-4">
            <div class="widget-item" style="padding-top:0px;margin-top:0px;">
                        <div class="widget-main-title">
                            <h4 class="widget-title">Notice Board</h4>
                        </div> <!-- /.widget-main-title -->
                        <marquee id="dvNotice" FACE="courier" BEHAVIOR="SCROLL" height="247px" onmouseout="this.setAttribute('scrollamount', 2, 0);" onmouseover="this.setAttribute('scrollamount', 0, 0);" scrollamount="2" direction="up" style="text-align: left;">
                            <%--<div class="widget-inner" id="dvNotice">

                            </div> --%><!-- /.widget-inner -->
                        </marquee>
                    <!-- /.request-information -->
            </div> <!-- /.widget-item -->
        </div>

JQuery/JavaScript - jQuery / JavaScript-

$(document).ready(function () {
PageMethods.loadNotice('', loadNoticeSuccess);
});
function loadNoticeSuccess(result) {
        $("#dvNotice").html('');
        var html = "";
        for (var i = 0; i < result.length; i++) {
            var month_value = result[i].PublishedDate.getMonth();
            var day_value = result[i].PublishedDate.getDate();
            html += "<div class=\"event-small-list clearfix\">";
            html += "<div class=\"calendar-small\"><span class=\"s-month\">" + months[month_value] + "</span><span class=\"s-date\">" + day_value + "</span></div>";
            //html += "<div class=\"event-small-details\"><h5 class=\"event-small-title\"><a href=\"event-single.html\">" + result[i].Title + "</a></h5><p class=\"event-small-meta small-text\">" + result[i].Description + "</p></div></div>";
            html += "<div class=\"event-small-details\"><h5 class=\"event-small-title\"><a href=\"Pages/NoticeBoard/NoticeDetails.aspx?noticeid=" + result[i].NoticeID + "\">" + result[i].Title + "</a></h5><p class=\"event-small-meta small-text\"></p></div></div>";
        }
        html += "<div class=\"event-small-list clearfix\" style=\"text-align:right;padding:0;\"><a href=\"Pages/NoticeBoard/NoticeBoard.aspx\">More</a></div>";
        $("#dvNotice").append(html);
    }

C# Code Behind for loading the notice board - 用于加载布告栏的C#代码后面-

[WebMethod]
    public static List<Notice> loadNotice(string value)
    {
        IList<Notice> notice = NoticeManager.GetTopPublishedNotice();
        if (notice == null)
        {
            notice = new List<Notice>();
        }
        return notice.ToList();
    }

I assume you are using some CMS to add these notices. 我假设您正在使用一些CMS添加这些通知。 One option is to check difference between notice/content creation time and now on page load. 一种选择是检查通知/内容创建时间与现在页面加载之间的差异。 This can be done easily through javascript get Time function. 这可以通过javascript get Time函数轻松完成。

If you don't have such value then simply add it to you CMS, to store metadata of creation date and time. 如果您没有这样的值,则只需将其添加到CMS中即可存储创建日期和时间的元数据。 Most CMS I have encountered have such possibilities. 我遇到的大多数CMS都有这样的可能性。

Hope this theory will help. 希望这个理论会有所帮助。

Can you please try this code after appending the html to the div dvNotice , 将html附加到div dvNotice之后,能否请您尝试以下代码,

setTimeout(function () {
        $('#dvNotice').css('color','yellow'); 
 }, 172800000);

I'm leveraging the function from this answer to calculate the difference of two dates, based on the assumption that 48 hours == 2 days 我基于48小时== 2天的假设,利用此答案中的函数来计算两个日期的差

var _MS_PER_DAY = 1000 * 60 * 60 * 24;

// a and b are javascript Date objects
function dateDiffInDays(a, b) {
  // Discard the time and time-zone information.
  var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
  var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());

  return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}

// tweaked OP code
function loadNoticeSuccess(result) {
    $("#dvNotice").html('');
    var html = "";
    for (var i = 0; i < result.length; i++) {
        var isPast48Hours = dateDiffInDays(new Date(), result[i].PublishedDate) > 2;    
        if (isPast48Hours) { /*setup html for default color*/ }        
        else { /*setup html for red color */ }

        // rest of code, using the html you setup above for the variable color

    }    
}

What's missing here is a real-time verification (and reaction, switching the colors) of the age of the entry: if the page is loaded 47 hours and 59 minutes after the published date, the entry will be red and stay red . 这里缺少的是条目寿命的实时验证(以及反应,更改颜色):如果页面在发布日期之后47小时59分钟加载,则该条目将为红色, 并保持红色

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

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