• JS年月日下拉列表


    // JavaScript Document
    var $ = function(id) { return ('string' == typeof id ? document.getElementById(id) : id); };
    var Extend = function(destination, source) {
    for(var pro in source) {
    destination[pro] = source[pro];
    }
    return destination;
    }
    var addEvent = function(obj, type, fn) {
    if(obj.addEventListener) {
    obj.addEventListener(type, fn, false);
    return true;
    }else if(obj.attachEvent) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){
    obj['e'+type+fn](window.event);
    }
    obj.attachEvent('on'+type, obj[type+fn]);
    return true;
    }
    return false;
    }

    function DateSelector(idYear, idMonth, idDay, options) {
    var D = new Date();
    this.year = $(idYear);
    this.month = $(idMonth);
    this.day = $(idDay);
    this.nowYear = D.getFullYear();
    this.nowMonth = D.getMonth() + 1;
    this.nowDate = D.getDate();
    Extend(this, this.setOptions(options));
    };
    DateSelector.prototype = {
    setOptions: function(options){
    // 默认项
    this.options = {
    floorYear: 100, // 距当前年份前100年
    ceilYear: 0, // 距当前年份后0年
    onStart: function(){}, // 前置事件
    onEnd: function(){} // 结束事件
    };
    return Extend(this.options, options || {});
    },
    //创建option
    createOption: function(container, start, end, sign){
    sign = sign || start;
    var _num = parseInt(end-start+1, 10); container.options.length = _num;
    for(var i = 0; i < _num; i++) {
    container.options[i].text = container.options[i].value = start + i;
    }
    container.selectedIndex = (sign-start >= _num ? _num-1 : sign-start);
    },
    // 获取年月对应的日期数
    getDate: function(y, m){
    return new Date(y, m, 0).getDate();
    },
    // 获取option-text
    getSelText: function(sel) {
    return sel.options[sel.selectedIndex].text;
    },
    // 年月日切换
    changeDate: function(bindO){
    var _this = this;
    addEvent(bindO, 'change', function(){
    var y = _this.getSelText(_this.year), m = _this.getSelText(_this.month), d = _this.getSelText(_this.day);
    _this.createOption(_this.day, 1, _this.getDate(y, m), d);
    _this.onEnd();
    });

    },
    // 绑定相应事件
    bindEvents: function(){
    var _this = this;
    this.changeDate(this.year); this.changeDate(this.month);
    addEvent(this.day, 'change', function(){ _this.onEnd(); });
    },
    // 代码初始化
    init: function(){
    var _startYear = parseInt(this.nowYear - this.floorYear, 10);
    var _endYear = parseInt(this.nowYear + this.ceilYear, 10);
    var _endDate = this.getDate(this.nowYear, this.nowMonth, 0);
    this.createOption(this.year, _startYear, _endYear, this.nowYear);
    this.createOption(this.month, 1, 12, this.nowMonth);
    this.createOption(this.day, 1, _endDate, this.nowDate);
    this.bindEvents();
    this.onStart();
    }
    };

  • 相关阅读:
    hdu6354 杭电第五场 Everything Has Changed 计算几何
    hdu6351 Beautiful Now 杭电第五场 暴力枚举
    牛客多校第六场 J Heritage of skywalkert 随即互质概率 nth_element(求最大多少项模板)
    百度之星资格赛 调查问卷 bitset模板(直接将字符串转化成二进制数组并可以计算出十进制值)
    百度之星资格赛 子串查询 线段树
    牛客多校第五场 F take 期望转化成单独事件概率(模板) 树状数组
    牛客多校第五场 E room 二分图匹配 KM算法模板
    牛客第五场多校 J plan 思维
    idhttp提交post
    centos6.2 shutdown now关机进入单用户模式
  • 原文地址:https://www.cnblogs.com/hasayaki/p/2888437.html
Copyright © 2020-2023  润新知