简体   繁体   English

通过jquery调用函数并将结果输出到razor html中的div

[英]call a function through jquery and output results to div in razor html

I'm working with twitter api and created_at property. 我正在使用twitter api和created_at属性。 Specifically i'm trying to get the timestamp to posted X seconds, minutes, hours, days, weeks ago. 具体来说,我正在尝试将时间戳记发布X秒钟,几分钟,几小时,几天,几周之前。

I think i have the right function written, I'm just unsure how to call the function. 我认为我编写了正确的函数,只是不确定如何调用该函数。 I've attempted to put my own attribute on a div and use jquery to find that div. 我试图将自己的属性放在div上,并使用jquery查找该div。 now i just need to know how to call the function. 现在我只需要知道如何调用该函数。

<div style="text-align: left" data-utc="@Model.FullTweetData.CreatedAt"></div>

<script src="/javascript/jquery-2.0.3.min.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function ($) {

        $("div[data-utc=@Model.FullTweetData.CreatedAt]")

        function parseTwitterDate(tdate) {
            var system_date = new Date(Date.parse(tdate));
            var user_date = new Date();
            if (K.ie) {
                system_date = Date.parse(tdate.replace(/( \+)/, ' UTC$1'))
            }
            var diff = Math.floor((user_date - system_date) / 1000);
            if (diff < 60) { return "less than a minute ago"; }
            if (diff <= 90) { return "one minute ago"; }
            if (diff <= 3540) { return Math.round(diff / 60) + " minutes ago"; }
            if (diff <= 5400) { return "1 hour ago"; }
            if (diff <= 86400) { return Math.round(diff / 3600) + " hours ago"; }
            if (diff <= 129600) { return "1 day ago"; }
            if (diff < 604800) { return Math.round(diff / 86400) + " days ago"; }
            if (diff <= 777600) { return "1 week ago"; }
            return "on " + system_date;
        }
        var K = function () {
            var a = navigator.userAgent;
            return {
                ie: a.match(/MSIE\s([^;]*)/)
            }
        }();
    });
</script>

any help would be great 任何帮助都会很棒

get value of the attribute and store it in a variable: 获取属性的值并将其存储在变量中:

var timeThing = $("div").attr('data-utc');

then call the function and pass the arg: 然后调用该函数并传递arg:

parseTwitterDate(timeThing);

I am going to guess that just selecting all divs is not going to give you the result you are looking for so you may consider attaching id's or classes to the divs to make sure you are selecting the right one: 我会猜测,仅选择所有div并不会为您提供所需的结果,因此您可以考虑将id或类附加到div上,以确保您选择了正确的div:

HTML: HTML:

<div class="twitterDates" data-utc="@Model.FullTweetData.CreatedAt"></div>

Then you can select all divs with that class, iterate through each one and call the parse date function for each one. 然后,您可以选择该类的所有div,依次遍历每个div,然后为每个div调用解析日期函数。

js: js:

$('div.twitterDates').each(function(){
  var timeThing = $(this).attr('data-utc);

  parseTwitterDate(timeThing);  
});

also since you probably want the return value of the function put somewhere you might consider something like this: 另外,由于您可能希望将函数的返回值放在某个位置,因此您可以考虑以下内容:

$('div.twitterDates').each(function(){
  var timeThing = $(this).attr('data-utc);

  $(this).text(parseTwitterDate(timeThing));
});

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

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