jquery开发模板

模板

/**
 * @author hp
 *
 * 初始化:
 * $("#xxx").hpSelect({
            data: [{
                id: '1',
                name: '我的实习实践计划(项目)',
                selected:true
            }, {
                id: '2',
                name: '全部'
            }],
            autotrue,
            '200',
            height:'30',
            optionBoxWidth: '200',
            callBack: function() {
              //  alert($("#select").hpSelect("getName"));
            }
        });
 * 方法:
 * 获取ID$("#xxx").hpSelect("getId")
 * 获取name$("#xxx").hpSelect("getName")
 *
 * 设置ID$("#xxx").hpSelect("setId",data)
 * 设置多个ID$("#xxx").hpSelect("setIds",arry)
 * 设置Name$("#xxx").hpSelect("setName",name)
 * 设置Name$("#xxx").hpSelect("setData",data);异步设值,下拉内容
 *
 * @date 2017-02-08
 */
(function($) {
    //初始化对象可设置的属性
    var defaults = {
        data: "",
        auto false,      //是否固定宽度,true 为自动宽度,false 为固定宽度
         '100',          //固定宽度时,设置select的宽度
        maxWidth: '200',       //自动宽度时候,设置最大select的宽度
        optionBoxWidth: '100', //设置下拉最大宽度
        height: "30",          //设置select高度
        placeholder:"请输入",  //输入提示语
        callBack: function() {}//回调函数
    };
    var methods = {
        init: function(options) {
            $(this).hpSelect('setOptions', options);
            return this;
        },
        /**
         * 设置运行参数
         */
        setOptions: function(options) {
            // 尝试去获取settings,如果不存在,则返回“undefined”
            var settings = $(this).data('hpSelect');
            // 如果获取settings失败,则根据options和default创建它
            if (!settings)
                settings = {};
            settings = $.extend({}, defaults, options);
            // 保存我们新创建的settings
            $(this).data('hpSelect', settings);
            return this;
        }

    };
    $.fn.hpSelect = function(method) {
        //假如方法存在
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
            //假如方法不存在,直接调用methods的init方法
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
            //否则报错。方法名不正确
        } else {
            alert('错误: ' + method + ' 不是本插件的方法!');
        }
    };

})(jQuery);
原文地址:https://www.cnblogs.com/hupan508/p/7463297.html