1.javascript
jQuery是一款很是優秀的腳本框架,其豐富的控件使用起來也很是簡單,配置很是靈活。下面作一個使用日期插件datapicker的例子。 css
一、下載jQuery核心文件就不用說了吧,datepicker是輕量級插件,只需jQuery的min版本就好了,而後到官網http://jqueryui.com/download下載jquery-ui壓縮包(能夠選擇喜歡的theme),裏面就包含對datepicker的支持,固然您也能夠網站http://marcgrabanski.com/pages/code/jquery-ui-datepicker下載datepicker,包括ui.core.js和ui.datepicker.js。html
二、在HTML中引用下載下來的js文件: java
-
- <mce:script src="js/jquery.1.4.2.js" mce_src="js/jquery-1.5.1.min.js" type="text/JavaScript"></mce:script>
- <mce:script src="js/jquery.ui.core.js" mce_src="js/jquery.ui.core.js" type="text/javascript"></mce:script>
- <mce:script src="js/jquery.ui.datepicker.js" mce_src="js/jquery.ui.datepicker.js" type="text/javascript"></mce:script>
3.在HTML中引入默認樣式表文件,這個文件在ui壓縮包中。若是在官網下載,首頁就有這個CSS文件下載,也可選擇其餘皮膚的CSS。jquery
- k type="text/css" rel="stylesheet" href="css/jquery-ui-1.8.13.custom.css" mce_href="css/jquery-ui-1.7.3.custom.css" />
4.在HTML中插入文本域,最好設置成只讀,不接受用戶的手動輸入,防止格式混亂,以id標記好。api
- <input type="text" id="selectDate" readonly="readonly"/>
5.編寫js代碼,實現最終效果。框架
- $(document).ready(function() {
- $('#selectDate').datepicker();
- });
效果以下圖:dom
![](http://static.javashuo.com/static/loading.gif)
這裏只是作了一個最基本的日期控件,咱們還須要以中文顯示,限制日期選擇範圍等需求,稍微修改js代碼:網站
- <mce:script type="text/javascript"><!--
-
- $(function(){
- $("#selectDate").datepicker({
- numberOfMonths:1,
- showButtonPanel:true,
- dateFormat: 'yy-mm-dd',
- clearText:"清除",
- closeText:"關閉",
- yearSuffix: '年',
- showMonthAfterYear:true,
- defaultDate:'2011-03-10',
- minDate:'2011-03-05',
- maxDate:'2011-03-20',
- monthNames: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
- dayNames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],
- dayNamesShort: ['週日','週一','週二','週三','週四','週五','週六'],
- dayNamesMin: ['日','一','二','三','四','五','六'],
- onSelect: function(selectedDate) {
- alert(selectedDate);
- }
- });
- });
-
效果以下:ui
![](http://static.javashuo.com/static/loading.gif)
這裏基本上就知足咱們使用的須要了。datepicker控件默認是英文的,能夠在構造datepicker時經過monthNames、dayNames屬性來指定月、日的中文顯示值,可是每次使用是都配置這些屬性難免太麻煩了,能夠增長一個js文件將中文配置都放在裏面,每次使用直接引用便可,這裏放在jquery.ui.datepicker-zh-CN.js中,內容以下:
- jQuery(function($){
- $.datepicker.regional['zh-CN'] = {
- closeText: '關閉',
- prevText: '<上月',
- nextText: '下月>',
- currentText: '今天',
- monthNames: ['一月','二月','三月','四月','五月','六月',
- '七月','八月','九月','十月','十一月','十二月'],
- monthNamesShort: ['一','二','三','四','五','六',
- '七','八','九','十','十一','十二'],
- dayNames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],
- dayNamesShort: ['週日','週一','週二','週三','週四','週五','週六'],
- dayNamesMin: ['日','一','二','三','四','五','六'],
- weekHeader: '周',
- dateFormat: 'yy-mm-dd',
- firstDay: 1,
- isRTL: false,
- showMonthAfterYear: true,
- yearSuffix: '年'};
- $.datepicker.setDefaults($.datepicker.regional['zh-CN']);
- });
6.在頁面中引入中文插件
- <!-- 添加中文支持-->
- <mce:script src="js/jquery.ui.datepicker-zh-CN.js" mce_src="js/jquery.ui.datepicker-zh-CN.js" type="text/javascript"></mce:script>
完整的頁面代碼以下:
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <HTML>
- <HEAD>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <TITLE>日期控件datepicker</TITLE>
-
-
- <mce:script src="js/jquery.1.4.2.js" mce_src="js/jquery.1.4.2.js" type="text/javascript"></mce:script>
-
-
- <mce:script src="js/jquery.ui.core.js" mce_src="js/jquery.ui.core.js" type="text/javascript"></mce:script>
- <mce:script src="js/jquery.ui.datepicker.js" mce_src="js/jquery.ui.datepicker.js" type="text/javascript"></mce:script>
- <!-- 或者引入jquery ui包,其中也包含對datepicker的支持
- <mce:script src="js/jquery-ui-1.7.3.custom.min.js" mce_src="js/jquery-ui-1.7.3.custom.min.js" type="text/javascript"></mce:script>
- -->
-
-
- <link type="text/css" rel="stylesheet" href="css/jquery-ui-1.7.3.custom.css" mce_href="css/jquery-ui-1.7.3.custom.css" />
-
-
- <mce:script src="js/jquery.ui.datepicker-zh-CN.js" mce_src="js/jquery.ui.datepicker-zh-CN.js" type="text/javascript"></mce:script>
-
- <mce:script type="text/javascript"><!--
- //等待dom元素加載完畢.
- $(function(){
- $("#selectDate").datepicker({//添加日期選擇功能
- numberOfMonths:1,//顯示幾個月
- showButtonPanel:true,//是否顯示按鈕面板
- dateFormat: 'yy-mm-dd',//日期格式
- clearText:"清除",//清除日期的按鈕名稱
- closeText:"關閉",//關閉選擇框的按鈕名稱
- yearSuffix: '年', //年的後綴
- showMonthAfterYear:true,//是否把月放在年的後面
- defaultDate:'2011-03-10',//默認日期
- minDate:'2011-03-05',//最小日期
- maxDate:'2011-03-20',//最大日期
- //monthNames: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
- //dayNames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],
- //dayNamesShort: ['週日','週一','週二','週三','週四','週五','週六'],
- //dayNamesMin: ['日','一','二','三','四','五','六'],
- onSelect: function(selectedDate) {//選擇日期後執行的操做
- alert(selectedDate);
- }
- });
- });
-
- // --></mce:script>
- </HEAD>
- <BODY>
- <input type="text" id="selectDate" readonly="readonly"/>
- </BODY>
- </HTML>
注意:因爲jquery datepicker首頁http://marcgrabanski.com/articles/jquery-ui-datepicker上ui.core.js和ui.datepicker.js不是最新版本的,若是下載新版本jquery-ui-1.8.13中的css文件會形成日期控件不能顯示的問題,因此這裏使用了1.7.3的ui。簡單一點就是用jquery-ui的壓縮js。