擴展 jq 對象方法
阿新 • • 發佈:2018-04-12
mov new return self remove nop tex pid lose
//擴展Jquery對象方法 $.fn.extend({ //proTree樹結構 amTree: function (opts) { var Tree = function (element, opts) { this.element = element; //json數組 this.JSONArr = opts.arr; //沒有下級時的圖標 this.simIcon = opts.simIcon || "icon iconfont icon-qrcode";//樹展開時圖標 this.mouIconOpen = opts.mouIconOpen || "icon iconfont icon-reduce"; //樹折疊時圖標 this.mouIconClose = opts.mouIconClose || "icon iconfont icon-zengjia-copy-copy"; //回調函數 this.callback = opts.callback || function () { };//默認展開的級數 this.openLevel = opts.openLevel || 0; this.rootID = opts.rootID || "00000000-0000-0000-0000-000000000000" //初始化 this.init(); } //初始化事件 Tree.prototype.init = function () { //事件 this.JSONTreeArr = this.proJSON(this.JSONArr, this.rootID); this.treeHTML = this.proHTML(this.JSONTreeArr, 0); this.element.append(this.treeHTML); this.bindEvent(); } //生成樹形JSON數據 Tree.prototype.proJSON = function (oldArr, pid) { var newArr = []; var self = this; $.map(oldArr, function (item) { if (item.pid == pid) { var obj = { id: item.id, name: item.name } var child = self.proJSON(oldArr, item.id); if (child.length > 0) { obj.child = child } newArr.push(obj) } }) return newArr; }; //生成樹形HTML Tree.prototype.proHTML = function (arr, lv) { var self = this; var ulHtml = "<ul class=‘treeUl tree‘ " + ((lv <= self.openLevel) ? "" : "style=‘display:none;‘") + ">"; $.map(arr, function (item) { var lihtml = "<li>"; if (item.child && item.child.length > 0) { var currLv = lv + 1; lihtml += "<i ischek=‘" + ((lv < self.openLevel) ? "true" : "false") + "‘ class=‘" + ((lv < self.openLevel) ? self.mouIconOpen : self.mouIconClose) + "‘ style=‘font-size:16px;‘ ></i>" + "<span id=‘" + item.id + "‘ title=‘" + item.name + "‘>" + item.name + "</span>" var _ul = self.proHTML(item.child, currLv); lihtml += _ul + "</li>"; } else { lihtml += "<i class=‘" + self.simIcon +" "+ "lay-bg" + "‘></i>" + "<span id=‘" + item.id + "‘ title=‘" + item.name + "‘>" + item.name + "</span>"; } ulHtml += lihtml; }) ulHtml += "</ul>"; return ulHtml; } Tree.prototype.bindEvent = function () { var self = this; this.element.find(".treeUl li i").click(function () { var ischek = $(this).attr("ischek"); if (ischek == ‘true‘) { var treeUl = $(this).closest("li").children(".treeUl"); $(this).removeClass(self.mouIconOpen).addClass(self.mouIconClose) treeUl.hide(); $(this).attr("ischek", ‘false‘); } else if (ischek == ‘false‘) { var treeUl = $(this).closest("li").children(".treeUl"); treeUl.show(); $(this).removeClass(self.mouIconClose).addClass(self.mouIconOpen) $(this).attr("ischek", ‘true‘) } }); this.element.find(".treeUl li span").click(function () { var id = $(this).attr("id"); var name = $(this).text(); self.element.find(".treeUl li span").removeClass("curr"); $(this).addClass("curr"); self.callback(id, name) }) } return new Tree($(this), opts) } });
// 選項卡
$.fn.extend({ myList: function(opts){ var List = function (element, opts) { this.element = element; // 按鈕數組 this.btnArr = opts.btnArr; // 內容數組 this.contentArr = opts.contentArr; // 事件 this.oEvent = opts.oEvent || "click"; // 回調函數 this.callback = opts.callback || function () {} this.init(); } List.prototype.init = function () { this.listHtml = this.proHtml(this.btnArr,this.contentArr); this.element.append(this.listHtml); this.bindEvent(this.oEvent); } List.prototype.proHtml = function (btnArr,contentArr) { var olHtml = "<ol class = ‘clearfix‘>"; for (var i in btnArr){ var btnHtml = "<li>" + btnArr[i] + "</li>" olHtml += btnHtml; } olHtml += "</ol>"; var listHtml = olHtml; var ulHtml = "<ul class = ‘clearfix‘>"; for (var i in contentArr){ var conHtml = "<li>" + contentArr[i] + "</li>" ulHtml += conHtml; } ulHtml += "</ul>"; listHtml += ulHtml; return listHtml; } List.prototype.bindEvent = function (oEvent) { var self = this; self.element.find("ul li").eq(0).css("display","block"); if (oEvent == ‘click‘){ self.element.find("ol li").click(function () { self.element.find("ul li").eq($(this).index()).show().siblings().hide(); self.callback(); }) } if (oEvent == ‘hover‘){ self.element.find("ol li").hover(function () { self.element.find("ul li").eq($(this).index()).show().siblings().hide(); self.callback(); }) } } return new List($(this),opts); } }) $(function() { $(".list").myList({ btnArr: ["選項1","選項2","選項3"], contentArr: ["內容一","內容二","內容三"], oEvent: "hover" }); $(".tfboys").myList({ btnArr: ["王俊凱","王源","易烊千璽"], contentArr: ["王俊凱有駕照了","源源很剛","千璽酷酷噠"], callback: function () { } }); })
擴展 jq 對象方法