[英]Problems seeting up a simple countdown timer
誰能告訴我我做錯了什么? 我是jquery的新手,我想得到一些反饋。 基本上我想要的是某種倒數計時器,它會顯示事件發生前剩余的天數。 該活動是一個確定的日期。
謝謝您的幫助
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Almost Vacation</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$('document').on('ready', calc);
function calc(){
var myDate = new Date();
myDate.setMonth(05, 06);
var today = new Date();
today.getDay();
var x = myDate - today;
$('#aantal p').text(x);
}
</script>
<style type="text/css">
p {
color:red;
font-size:1.8em;
margin:-90px 10px 5px;
}
</style>
</head>
<body>
<img src="http://fed.cmd.hro.nl/upload/files/1011/y1/q4/w3/slapende_student.jpg" width="462" height="275" />
<p>Vacation starts in<span id="aantal"> </span> Days</p>
</body>
</html>
$('document').on('ready', calc);
應該:
$(document).ready(calc);
或者干脆:
$(calc);
$('document')
正在尋找<document>
類型的元素
而$(document)
用jQuery對象包裝document
節點。
它需要是:
$(function() {
var myDate = new Date();
myDate.setMonth(06, 06); //set date forward in time, not backward
var today = new Date();
var x = (myDate - today)/86400000;
$('#aantal').text(x); //append to the span, not the p that does not exists
});
除了gdoron指出的語法修正,你得到的范圍不正確。 首先,你想從today
減去myDate
而不是相反。 此外,此減法的結果是日期之間的毫秒數,因此您需要進行一些單位轉換以達到數天。 最后,你的jQuery選擇器是不正確的。
<script>
$(document).ready(calc);
function calc(){
var myDate = new Date();
myDate.setMonth(5, 6);
var today = new Date();
var x = (today - myDate)/(1000*60*60*24);
$('#aantal').text(x); // update this selector too!!
}
</script>
如果您決定要對此數字進行舍入,則可以執行以下操作:
var x = Math.ceil((today - myDate)/(1000*60*60*24));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.