我正在尝试刷新div的日期,我不确定为什么div在点击刷新时变得不可见。如何刷新点击div的日期。 JS fiddle demo
Jquery code
var monthId = (new Date).getMonth();
var dayNo=(new Date).getDate();
var monthName = GetMonthFullName(monthId);
var hours = new Date().getHours();
var minutes = new Date().getMinutes();
var seconds = new Date().getSeconds();
var orderDate = monthName + " " + dayNo +", "+ hours +":"+minutes+":"+seconds;
function GetMonthFullName(monthNumber) {
var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
return months[monthNumber-1]
}
$('#datetime').text(orderDate);
$('#refreshDetails').click(function(){
$('#datetime').text(orderDate);
$("#detailsRefreshCont").load(location.href + "#detailsRefreshCont > *");
});
HTML Code
<div class="linkText" id="detailsRefreshCont" >
<span id="datetime">
</span>
<span class="linkText" id="refreshDetails">Refresh</span>
</div>
答案 0 :(得分:3)
没有必要在那里使用.load(),试试
//move the logic to a function so that it can be resused
function getTime() {
var monthId = (new Date).getMonth();
var dayNo = (new Date).getDate();
var monthName = GetMonthFullName(monthId);
var hours = new Date().getHours();
var minutes = new Date().getMinutes();
var seconds = new Date().getSeconds();
return monthName + " " + dayNo + ", " + hours + ":" + minutes + ":" + seconds;
}
function GetMonthFullName(monthNumber) {
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
return months[monthNumber - 1]
}
$('#datetime').text(getTime());
$('#refreshDetails').click(function () {
//there is no need to use load here, just call the function which returns the time
$('#datetime').text(getTime());
});
演示:Fiddle
在你的情况下,你试图重新加载容器元素的内容,这是失败的,因此元素detailsRefreshCont
的内容被清空。