Jquery時間外掛
阿新 • • 發佈:2018-11-22
使用一個閉包自執行函式編寫外掛,傳入JQuery物件。
(function($){ $.fn.extend({ clock:function(options){ var defaults = {height:'50px',width:'130px',fontSize:'16px', backgroundColor:'white',padding:'10px'}; options = $.extend({},defaults, options); var now = new Date(); var year = now.getFullYear(); var month = now.getMonth()+1; var day = now.getDate(); var hour = now.getHours(); var min = now.getMinutes(); var div1 = "<div>" + year + "年" + month + "月" + day + "日"+ "</div>"; var div2 = "<div>" + hour + "時" + min + "分" + "</div>"; return $(this).each(function(){ $(this).css({height:options.height,width:options.width, fontSize:options.fontSize,backgroundColor:options.backgroundColor, padding:options.padding,textAlign:'center'}); $(this).append(div1); $(this).append(div2); }); } }); })(window.jQuery); $(document).ready(function(){ $(".time").clock(); })