參考自: https://blog.csdn.net/jrn1012/article/details/76043177css
github地址: https://github.com/bill1012/bootstrap-tabhtml
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"> <link rel="stylesheet" href="font-awesome/css/font-awesome.min.css"> <!--bootstrap-tab樣式--> <link rel="stylesheet" href="../css/bootstrap-tab.css"> <script src="jquery/jquery-1.8.3.min.js"></script> <script src="bootstrap/js/bootstrap.min.js"></script> <script src="../js/bootstrap-tab.js"></script> <div id="tabContainer"></div> <script> <!-- 主要是bootstrap-tab.css和bootstrap-tab.js 這個有bug,我這三個面板的頁面通過後臺跳轉的,渲染會出異常 第一個面板會出現第二個面板的內容,第二個面板會出現第三個面板的內容.. --> var busiPresiteDevchkId = '${busiPresiteDevchkId}'; var vehicleid = '${vehicleid}'; var machinecode = '${machinecode}'; console.log(machinecode); $("#tabContainer").tabs({ data: [{ id: 'edit', text: '編輯修改', url: "${ctxPath}/busiPresiteDevchk/busiPresiteDevchk_update/" + busiPresiteDevchkId }, { id: 'carType', text: '車型', url: "${ctxPath}/basVehicle/basVehicle_info/" +vehicleid }, { id: 'machines', text: '設備', url: "${ctxPath}/basStationMachine/basStationMachine_info/" + machinecode }], //showIndex: 1,//默認顯示的位置0 loadAll: false }) //$("#tabContainer").data("tabs").addTab({id: 'test', text: 'addTab', closeable: true, url: '${ctxPath}/blackboard'}) </script>
(function ($, window, document, undefined) { 'use strict'; var pluginName = 'tabs'; //入口方法 $.fn[pluginName] = function (options) { var self = $(this); if (this == null) return null; var data = this.data(pluginName); if (!data) { data = new BaseTab(this, options); self.data(pluginName, data); } return data; }; var BaseTab = function (element, options) { this.$element = $(element); this.options = $.extend(true, {}, this.default, options); this.init(); } //默認配置 BaseTab.prototype.default = { showIndex: 0, //默認顯示頁索引 loadAll: false,//true=一次所有加在頁面,false=只加在showIndex指定的頁面,其餘點擊時加載,提升響應速度 } //結構模板 BaseTab.prototype.template = { ul_nav: '<ul id="myTab" class="nav nav-tabs"></ul>', ul_li: '<li><a href="#{0}" data-toggle="tab"><span>{1}</span></a></li>', ul_li_close: '<i class="fa fa-remove closeable" title="關閉"></i>', div_content: '<div class="tab-content"></div>', div_content_panel: '<div class="tab-pane fade" id="{0}"></div>' } //初始化 BaseTab.prototype.init = function () { if (!this.options.data || this.options.data.length == 0) { console.error("請指定tab頁數據"); return; } //當前顯示的顯示的頁面是否超出索引 if (this.options.showIndex < 0 || this.options.showIndex > this.options.data.length - 1) { console.error("showIndex超出了範圍"); //指定爲默認值 this.options.showIndex = this.default.showIndex; } //清除原來的tab頁 this.$element.html(""); this.builder(this.options.data); } //使用模板搭建頁面結構 BaseTab.prototype.builder = function (data) { var ul_nav = $(this.template.ul_nav); var div_content = $(this.template.div_content); for (var i = 0; i < data.length; i++) { //nav-tab var ul_li = $(this.template.ul_li.format(data[i].id, data[i].text)); //若是可關閉,插入關閉圖標,並綁定關閉事件 if (data[i].closeable) { var ul_li_close = $(this.template.ul_li_close); ul_li.find("a").append(ul_li_close); ul_li.find("a").append(" "); } ul_nav.append(ul_li); //div-content var div_content_panel = $(this.template.div_content_panel.format(data[i].id)); div_content.append(div_content_panel); } this.$element.append(ul_nav); this.$element.append(div_content); this.loadData(); this.$element.find(".nav-tabs li:eq(" + this.options.showIndex + ") a").tab("show"); } BaseTab.prototype.loadData = function () { var self = this; //tab點擊即加載事件 //設置一個值,記錄每一個tab頁是否加載過 this.stateObj = {}; var data = this.options.data; //若是是當前頁或者配置了一次性所有加載,不然點擊tab頁時加載 for (var i = 0; i < data.length; i++) { if (this.options.loadAll || this.options.showIndex == i) { if (data[i].url) { $("#" + data[i].id).load(data[i].url,data[i].param); this.stateObj[data[i].id] = true; } else { console.error("id=" + data[i].id + "的tab頁未指定url"); this.stateObj[data[i].id] = false; } } else { this.stateObj[data[i].id] = false; (function (id, url,paramter) { self.$element.find(".nav-tabs a[href='#" + id + "']").on('show.bs.tab', function () { if (!self.stateObj[id]) { $("#" + id).load(url,paramter); self.stateObj[id] = true; } }); }(data[i].id, data[i].url, data[i].paramter)) } } //關閉tab事件 this.$element.find(".nav-tabs li a i.closeable").each(function (index, item) { $(item).click(function () { var href = $(this).parents("a").attr("href").substring(1); if(self.getCurrentTabId()==href){ self.$element.find(".nav-tabs li:eq(0) a").tab("show"); } $(this).parents("li").remove(); $("#" + href).remove(); }) }); } //新增一個tab頁 BaseTab.prototype.addTab=function (obj) { var self=this; //nav-tab var ul_li = $(this.template.ul_li.format(obj.id, obj.text)); //若是可關閉,插入關閉圖標,並綁定關閉事件 if (obj.closeable) { var ul_li_close = $(this.template.ul_li_close); ul_li.find("a").append(ul_li_close); ul_li.find("a").append(" "); } this.$element.find(".nav-tabs:eq(0)").append(ul_li); //div-content var div_content_panel = $(this.template.div_content_panel.format(obj.id)); this.$element.find(".tab-content:eq(0)").append(div_content_panel); //$("#" + obj.id).load(obj.url,obj.paramter); //this.stateObj[obj.id] = true; this.stateObj[obj.id] = false; (function (id, url,paramter) { self.$element.find(".nav-tabs a[href='#" + id + "']").on('show.bs.tab', function () { if (!self.stateObj[id]) { $("#" + id).load(url,paramter); self.stateObj[id] = true; } //this.$element.find(".nav-tabs a[href='#" + obj.id + "']").tab("show"); }); }(obj.id, obj.url, obj.paramter)) if(obj.closeable){ this.$element.find(".nav-tabs li a[href='#" + obj.id + "'] i.closeable").click(function () { var href = $(this).parents("a").attr("href").substring(1); if(self.getCurrentTabId()==href){ self.$element.find(".nav-tabs li:eq(0) a").tab("show"); } $(this).parents("li").remove(); $("#" + href).remove(); }) } } //根據id獲取活動也標籤名 BaseTab.prototype.find=function (tabId) { return this.$element.find(".nav-tabs li a[href='#" + tabId + "']").text(); } // 刪除活動頁 BaseTab.prototype.remove=function (tabId) { var self=this; $("#" + tabId).remove(); this.$element.find(".nav-tabs li a[href='#" + tabId + "']").parents("li").remove(); } // 從新加載頁面 BaseTab.prototype.reload=function (obj) { var self=this; if(self.find(obj.id)!=null){ $("#" + obj.id).remove(); this.$element.find(".nav-tabs li a[href='#" + obj.id + "']").parents("li").remove(); self.addTab(obj); }else{ self.addTab(obj); } } //根據id設置活動tab頁 BaseTab.prototype.showTab=function (tabId) { this.$element.find(".nav-tabs li a[href='#" + tabId + "']").tab("show"); } //獲取當前活動tab頁的ID BaseTab.prototype.getCurrentTabId=function () { var href=this.$element.find(".nav-tabs li.active a").attr("href"); href=href.substring(1); return href; } String.prototype.format = function () { if (arguments.length == 0) return this; for (var s = this, i = 0; i < arguments.length; i++) s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]); return s; }; })(jQuery, window, document)
這個渲染有問題,只能設置成一次加載一個tab,一次加載多個渲染有問題,好比a頁面的內容到了b頁面,b頁面的內容到了c頁面,c頁面的內容到了a頁面.jquery
有大佬看了把這個問題解決了,請在評論區指點在下一二.在下是個菜雞,什麼都不懂的!git