Show toolbar

2011年12月13日 星期二

JavaScript Countdown

標題:JS Countdown
HTML (index.html):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<span id='clock'></span>
<script type="text/javascript">
var reciprocalboxy = new function() {
return {
endDay : new Date("2030/1/1 00:00:00"),
show : function() {
var today = new Date();
var timeRemain = this.endDay - today;
var D = parseInt(timeRemain/1000/60/60/24);
var h = parseInt((timeRemain/1000/60/60)%24);
var m = parseInt((timeRemain/1000/60)%60);
var s = parseInt((timeRemain/1000)%60);
document.getElementById('clock').innerHTML =
D + "日" + h + "點" + m + "分" + s + "秒";
},
init : function() {
this.show();
window.setInterval(function() {
reciprocalboxy.show();
}, 1000);
}
};
};
reciprocalboxy.init();
</script>
範例:
1734日18點2分45秒

說明:
簡易倒數計時器範例。

2011年12月11日 星期日

JavaScript dollar sign

標題:自定義錢字號函式
HTML (index.html):
1
2
3
4
5
6
7
8
9
10
11
<div id="btn">
<button>Click1</button>
<button>Click2</button>
<button>Click3</button>
</div>
<script type="text/javascript">
function $(id) {return document.getElementById(id);}
$('btn').onclick = function(e) {
e.target.innerHTML = "Clicked";
};
</script>
範例:

說明:
自定義一個錢字號指向id之函式,讓程式更精簡。