锘??xml version="1.0" encoding="utf-8" standalone="yes"?>91啪国产在线,欧美日韩国产精品专区,亚洲高清一区二http://blogjava.net/silvermyth/category/55192.htmlzh-cnMon, 10 Apr 2017 07:12:53 GMTMon, 10 Apr 2017 07:12:53 GMT60JQuery鎻掍歡緙栧啓紺轟緥http://www.aygfsteel.com/silvermyth/articles/432432.htmlGavin LiGavin LiWed, 05 Apr 2017 09:35:00 GMThttp://www.aygfsteel.com/silvermyth/articles/432432.htmlhttp://www.aygfsteel.com/silvermyth/comments/432432.htmlhttp://www.aygfsteel.com/silvermyth/articles/432432.html#Feedback0http://www.aygfsteel.com/silvermyth/comments/commentRss/432432.htmlhttp://www.aygfsteel.com/silvermyth/services/trackbacks/432432.html     瑕佹兂緙栧啓涓涓狫query鎻掍歡錛岄鍏堝繀欏誨Jquery鏈夋墍浜嗚В錛屽挨鍏舵槸浠ヤ笅鍑犱釜鐭ヨ瘑鐐癸細(xì)
  •     鐭ラ亾錛坒unction($){...}錛?jQuery)鏄粈涔堟剰鎬?br /> 錛坒unction($){...}錛?jQuery)瀹氫箟浜嗕竴涓嚱鏁幫紝騫朵笖浠Query涓哄弬鏁拌繘琛屼簡璋冪敤錛屼竴鑸儏鍐典笅錛岃繖鏄疄鐜癹Query鎻掍歡鐨勬爣鍑嗗Э鍔匡紝瀵硅繖涓湁鍏磋叮鐨勫悓瀛﹀彲浠ョ戶緇繁鍏ョ爺絀躲?br />
  •     $.proxy(fun, context)
    璇ユ柟娉曡繑鍥炰竴涓猣un鍑芥暟鐨勪唬鐞嗭紝榪斿洖鐨勪唬鐞嗗嚱鏁板姛鑳藉拰fun鐩稿悓錛屼笉鍚岀殑鏄繑鍥炲嚱鏁頒嬌鐢╟ontext鍙傛暟浣滀負(fù)璋冪敤涓婁笅鏂囥傝繖孌佃瘽鏈夌偣緇曞彛錛屼笉鏄庣櫧鐨勫悓瀛﹁鍙傝冿細(xì)
    http://www.cnblogs.com/hongchenok/p/3919497.html
  •     $.each(arr, function(){...})
    璇ユ柟娉曞湪姣忎釜arr涓殑鍏冪礌涓婃墽琛宑allback鏂規(guī)硶錛屽瓨鍦ㄥ緢澶氬彉浣撱傝緇嗚鍙傝http://www.jb51.net/article/24581.htm
  •     $.extend(deep, obj, obj1, ..)
    Jquery鎻愪緵鐨勭敤鏉ユ墿灞曚竴涓璞$殑鏂規(guī)硶錛屽嵆灝嗗璞bj1鍚堝茍鍒板璞bj錛屼竴鑸鐢ㄦ潵鍚慾Query瀵硅薄涓坊鍔犳柟娉曘傝緇嗚鍙傝僪ttp://www.cnblogs.com/tianguook/p/4084061.html
  •     $.data(dom, data-name, data-value)
    Jquery鎻愪緵鐨勭敤鏉ョ粦瀹氬璞″埌dom瀵硅薄涓殑鏂規(guī)硶錛屽鏋滄病鏈夊畾涔塪ata-value鍙傛暟錛屾槸璇誨彇data-name鐨勫鹼紱濡傛灉瀹氫箟浜哾ata-value鍒欐槸璁劇疆data-name鐨勫箋?br />
      鏈変簡浠ヤ笂鐭ヨ瘑錛屾垜浠潵瀹炵幇涓涓猄elect鐨凧query鎻掍歡錛屽彲浠ユ柟渚跨殑鍒涘緩銆佹坊鍔犻夐」銆佸垹闄ら夐」絳夌瓑銆備唬鐮佸涓嬶細(xì)
    /**
 * Created by gavinli on 17-3-30.
 
*/
;(function ($) {
    'use strict';

    var pluginName = 'myList';

    var _default = {};

    _default.settings = {};

    var MyList = function (element, options) {
        this.$element = $(element);
        this.init(options);
        return {
            init: $.proxy(this.init, this),
            add: $.proxy(this.add, this),
            remove: $.proxy(this.remove, this),
            list: $.proxy(this.list, this),
            clear: $.proxy(this.clear, this),
            getSelected: $.proxy(this.getSelected, this)
        }
    };

    MyList.prototype.init = function (options) {
        this.items = [];

        if (options.data) {
            if (typeof options.data === 'string') {
                options.data = $.parseJSON(options.data);
            }
            this.items = $.extend(true, [], options.data);
            delete options.data;
        }
        this.options = $.extend({}, _default.settings, options);
        this.render();
        this.subscribeEvents();
    };

    MyList.prototype.subscribeEvents = function () {
        //TODO:
    };

    MyList.prototype.add = function (items) {
        if (!(items instanceof Array)) {
            items = [items];
        }
        var _this = this;
        $.each(items, function (i, value) {
            _this.items.push(value);
        });
        this.filterDup();
        this.render();
    }

    //Remove all duplicated items
    MyList.prototype.filterDup = function () {
        var _this = this;
        var values = {}
        $.each(_this.items, function (i, value) {
            if (values[value]) {
                _this.items[i] = null;
            } else {
                values[value] = true;
            }
        });
    }

    MyList.prototype.remove = function (items) {
        var _this = this;
        var toBeRemoved = {};
        $.each(items, function (i, value) {
            toBeRemoved[value] = true;
        });
        $.each(_this.items, function (i, value) {
            if (toBeRemoved[value] == true) {
                _this.items[i] = null;
            }
        });
        this.render();
    }

    MyList.prototype.getSelected = function () {
        return this.$wrapper.val();
    }

    MyList.prototype.list = function (item) {
        var result = [];
        $.each(this.items, function (i, value) {
            if (value) {
                result.push(value);
            }
        });
        return result;
    }

    //Clear all items
    MyList.prototype.clear = function () {
        delete this.items;
        this.items = [];
        this.render();
    }

    MyList.prototype.render = function () {
        if (!this.initialized) {
            this.$wrapper = $(this.template.list);
            this.initialized = true;
        }
        //Append select element to $element
        this.$element.empty().append(this.$wrapper.empty());

        //Build select options
        this.buildList(this.items);
    }

    MyList.prototype.buildList = function (items) {
        var _this = this;
        $.each(items, function (i, value) {
            if (value) {
                var option = $(_this.template.item);
                option.append(value);
                _this.$wrapper.append(option);
            }
        });
    }

    MyList.prototype.template = {
        list: '<select multiple class="form-control"></select>',
        item: '<option></option>'
    };

    $.fn[pluginName] = function (options, args) {
        var result;
        this.each(function () {
            var _this = $.data(this, pluginName);
            if (typeof options === 'string') {
                if (!_this) {
                    //logError('Not initialized, can not call method : ' + options);
                }
                else if (!$.isFunction(_this[options]) || options.charAt(0) === '_') {
                    //logError('No such method : ' + options);
                }
                else {
                    if (!(args instanceof Array)) {
                        args = [args];
                    }
                    result = _this[options].apply(_this, args);
                }
            }
            else if (typeof options === 'boolean') {
                result = _this;
            }
            else {
                $.data(this, pluginName, new MyList(this, $.extend(true, {}, options)));
            }
        });
        return result || this;
    };

})(jQuery);
    涓嬮潰閽堝鍏朵腑鐨勫叧閿柟娉曡繘琛屽垎鏋愯瑙o細(xì)
    MyList鍑芥暟錛歁yList瀵硅薄鐨刢onstrunctor鏂規(guī)硶錛屾帴鍙梠ptions鍙傛暟錛坥ptions鍙傛暟鍖呭惈鎵鏈塷ptions鐨勬暟緇勶級(jí)
    MyList.prototype.init錛氭牴鎹畂ptions鐨刣ata鏋勫緩騫舵覆鏌揝elect鎺т歡
    MyList.prototype.add錛氭坊鍔爋ption鍒癝elect涓茍娓叉煋錛屽叾瀹價(jià)emove錛宭ist錛実etSelected鏂規(guī)硶澶у鑷鐮旂┒
    涓婇潰浠g爜涓紝鏈鏍稿績鐨勯儴鍒嗗湪浜庡浣曞皢MyList瀵硅薄鍜孌om鍏冪礌緇撳悎銆佸茍涓旀墿灞曞埌Jquery涓紝鍏蜂綋鍙傝冨涓嬫敞閲婁唬鐮侊細(xì)
    ////鎵╁睍jQuery鐨刾rototype瀵硅薄錛岃繖閲岀殑plugName絳変簬myList錛岀浉褰撲簬緇檍Query瀵硅薄娣誨姞浜嗕竴涓?myList"鏂規(guī)硶
    $.fn[pluginName] = function (options, args) { 
        var result;
        ////榪欓噷鐨則his鏄竴涓猨Query瀵硅薄
        this.each(function () {    
            //涓嬮潰鐨則his涓嶆槸jQuery瀵硅薄錛岃屾槸jQuery瀵硅薄涓殑Dom瀵硅薄
            //浠嶥om瀵硅薄涓幏鍙?data-myList"灞炴х粦瀹氱殑瀵硅薄
            var _this = $.data(this, pluginName);
            //options鏄柟娉曞悕錛屼緥濡?('#list1').MyList('add'錛孾])錛岃繖閲岀殑options絳変簬'add'
            if (typeof options === 'string') {
                if (!_this) {
                    //logError('Not initialized, can not call method : ' + options);
                }
                else if (!$.isFunction(_this[options]) || options.charAt(0) === '_') {
                    //logError('No such method : ' + options);
                }
                else {
                    if (!(args instanceof Array)) {
                        args = [args];
                    }
                    //璋冪敤MyList瀵硅薄鐨勬柟娉?/span>
                    result = _this[options].apply(_this, args);
                }
            }
            else if (typeof options === 'boolean') {
                result = _this;
            }
            else {
                //鍒涘緩MyList瀵硅薄騫剁粦瀹氬埌Dom瀵硅薄鐨刣ata-myList灞炴?/span>
                $.data(this, pluginName, new MyList(this, $.extend(true, {}, options)));
            }
        });
        return result || this;
    };
   濡備綍浣跨敤璇ユ彃浠剁殑鏂規(guī)硶濡備笅鎵紺猴細(xì)
    棣栧厛鍦╤tml涓畾涔変竴涓?lt;div id="list1"></div>錛岀劧鍚庤繖鏍蜂嬌鐢ㄥ畠錛?br />     //鍒涘緩涓涓猄elect鍖呭惈涓変釜options
$('#list1').MyList(['Tom','Mary','Alice']);
//娣誨姞鏂扮殑option
$('#list1').MyList('add', [['James','Richard']]);
//鍒犻櫎option
$('#list1').MyList('remove', [['Alice']]);
    鏈鍚庢垜浠彲浠ュ湪浠ヤ笂渚嬪瓙涓彂鐜板垱寤簀Query鎻掍歡鐨勬諱綋鎬濊礬錛?br />
  •     鑷畾涔夊璞★紝瀵硅薄涓寘鍚暟鎹拰jQuery瀵硅薄鏈韓
  •     瀹氫箟瀵硅薄鐨勬柟娉曪紝騫朵笖鏍規(guī)嵁瀵硅薄涓暟鎹殑鍙樺寲娓叉煋Dom瀵硅薄錛堥氳繃jQuery瀵硅薄鑾峰緱Dom瀵硅薄錛?/li>
  •     灝嗚鑷畾涔夊璞℃柟娉曟墿灞曞埌jQuery鍘熷瀷瀵硅薄涓?/li>
  •     鍒涘緩鑷畾涔夊璞★紝騫剁粦瀹氬埌jQuery涓璂om瀵硅薄鐨刣ata灞炴?br />
    閫氳繃浠ヤ笂瀹炵幇錛屾垜浠究鍙互鍍忎嬌鐢╦Query瀵硅薄涓鏍風(fēng)殑鏂瑰紡浣跨敤鎺т歡錛屽睆钄藉Dom鍏冪礌鐨勬搷浣滐紝綆鍗曞張鏂逛究

Gavin Li 2017-04-05 17:35 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 东至县| 高陵县| 兴隆县| 岑巩县| 桂林市| 开原市| 玉田县| 平利县| 苏尼特右旗| 石屏县| 固阳县| 潢川县| 保山市| 玉树县| 广德县| 铜陵市| 晋江市| 泉州市| 海伦市| 枣阳市| 成武县| 长葛市| 临朐县| 涟水县| 高雄市| 南华县| 蚌埠市| 微博| 中山市| 扬州市| 长阳| 濮阳县| 镇坪县| 阜南县| 木里| 长岭县| 什邡市| 靖边县| 灵丘县| 东台市| 射洪县|