Show toolbar

2011年12月13日 星期二

JavaScript Countdown

標題:JS Countdown
HTML (index.html):
<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>
範例:


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

2011年12月11日 星期日

JavaScript dollar sign

標題:自定義錢字號函式
HTML (index.html):
<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之函式,讓程式更精簡。