在div中刷新日期时间

时间:2014-04-23 19:59:11

标签: jquery html datetime razor refresh

我正在尝试刷新一个带有jquery ajax调用和datetime的加载记录的div。我无法刷新日期时间。我怎么能这样做

HMTL

 <div class="linkText" id="detailsRefreshCont" >
     @DateTime.Now.ToString("MMMM dd"), @DateTime.Now.ToString("H:mm") 
     <span class="linkText" id="refreshDetails">Refresh</span>
 </div>

的JavaScript

$('#refreshDetails').click(function () {
     loadDetailsData();
     $("#detailsRefreshCont").load(location.href + " #detailsRefreshCont > *");
});

2 个答案:

答案 0 :(得分:1)

location.href不返回页面

正确使用.load()方法

https://api.jquery.com/load/

 $("#detailsRefreshCont").load("datetime.html");

或: HTML

 <div class="linkText" id="detailsRefreshCont" >
 <span id="datetime">
  @DateTime.Now.ToString("MMMM dd"), @DateTime.Now.ToString("H:mm")
  </span> 
 <span class="linkText" id="refreshDetails">Refresh</span>

JAVASCRIPT

 $('#refreshDetails').click(function () {
 loadDetailsData();
 var now = new Date(), date = now.getDate(), month =  now.getMonth(), weekday=new Array(7), namemonth = new Array(12),day = now.getDay(), hour = now.getHours(), minute = now.getMinutes();
namemonth[0]="January";
namemonth[1]="February";
namemonth[2]="March";
namemonth[3]="April";
namemonth[4]="May";
namemonth[5]="June";
namemonth[6]="July";
namemonth[7]="August";
namemonth[8]="September";
namemonth[9]="October";
namemonth[10]="November";
namemonth[11]="December";
day -= 1; 
weekday[0]="Monday";
weekday[1]="Tuesday";
weekday[2]="Wednesday";
weekday[3]="Thursday";
weekday[4]="Friday";
weekday[5]="Saturday";
weekday[6]="Sunday";
 $("#datetime").html(namemonth[month]+ " "+weekday[day]+ ", " + hour +":"+ minute);
});   

答案 1 :(得分:1)

#detailsRefreshCont > *选择器似乎只带有带标签的元素,尝试用<span>之类的方式包装日期:

<div class="linkText" id="detailsRefreshCont" >
     <span>@DateTime.Now.ToString("MMMM dd"), @DateTime.Now.ToString("H:mm")</span>
     <span class="linkText" id="refreshDetails">Refresh</span>
</div>