ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
``` function Li(options) { function TabLiContent(options) { this._init(options); } TabLiContent.prototype = { // 数据初始化 _init : function(options) { this.tabIndex = options.tabIndex || 0; this.liNum = options.liNum || 0; this.container = options.container || null; this.index = options.index || 0; return this; }, // 数据校验 _checkData : function() { if (this.container == null) { throw new Error('have not container'); } return this; }, // 随机填充container _makeLi : function() { this._checkData(); var str = ''; str += '<li data-index='+this.index+' data-tab='+this.tabIndex+' class="li_con">' str += '<span data-index='+this.index+' data-tab='+this.tabIndex+' class="li_word">这是第' + this.tabIndex + 'tab栏的第' + this.liNum + '个li</span>' str += '<span class="list_ok" data-index='+this.index+' data-tab='+this.tabIndex+'>删除</span>' str += '</li>'; return str; }, // 获取当前对象的数据 _getObjSData:function(dom){ return dom.dataset; }, // 点击跳转详情 _openDetail: function(data) { var that = this; var data = that._getObjSData(data); console.log(data); alert('即将打开' +data.index+'商品详情的页面 '+data.tab+''); }, // 删除 _delListOne:function(data) { var that = this; var data = that._getObjSData(data); console.log(data); alert('即将删除此商品' +data.index+'') }, // 事件绑定[所有的点击事件通过事件委托方式绑定] _bindEvent : function(options) { var con = this.container; var that = this; con.onclick = function(ev) { var ev = ev || window.event;       var target = ev.target || ev.srcElement; switch(target.className){ case 'li_con': // 进入详情 that._openDetail(target); break; case 'list_ok': // 删除 that._delListOne(target); break; case 'li_word': // 进入详情 that._openDetail(target); break; } } }, main : function() { try { this._checkData(); this.container.innerHTML += this._makeLi(); } catch(e) { console.log(e); } } } return function(options) { var tabLiContent = new TabLiContent(options); tabLiContent.main(); tabLiContent._bindEvent(options); return tabLiContent; }(options); } ``` ``` var dom = document.querySelector('#listOne'); var dom1 = document.querySelector('#listTwo'); var dom2 = document.querySelector('#listThree') var json = [{ tabIndex : 0, liNum : 1, container : dom, index : 0 }, { tabIndex : 0, liNum : 2, container : dom, index : 1 }, { tabIndex : 0, liNum : 3, container : dom, index : 2 }, { tabIndex : 0, liNum : 4, container : dom, index : 3 }]; var json1 = [{ tabIndex : 1, liNum : 1, container : dom1, index : 0 }, { tabIndex : 1, liNum : 2, container : dom1, index : 1 }, { tabIndex : 1, liNum : 3, container : dom1, index : 2 }]; var json2 = [{ tabIndex : 2, liNum : 1, container : dom2, index : 0 }, { tabIndex : 2, liNum : 2, container : dom2, index : 1 }, { tabIndex : 2, liNum : 3, container : dom2, index : 2 }]; json.forEach(function(item) { Li(item); }) json1.forEach(function(item) { Li(item); }) json2.forEach(function(item){ Li(item); }) ```