Skip to content

Commit 32b864b

Browse files
committed
修复关闭当前窗口未激活前一个选项卡的BUG
1 parent 12d8473 commit 32b864b

File tree

1 file changed

+44
-35
lines changed

1 file changed

+44
-35
lines changed

jquery.addtabs.js

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@
4141
var title = $(this).attr('title') ? $(this).attr('title') : $.trim($(this).text());
4242
var url = $(this).attr('url');
4343
var content = options.content ? options.content : $(this).attr('content');
44-
var ajax = $(this).attr('ajax') ? true : false;
44+
var ajax = $(this).attr('ajax') === '1' || $(this).attr('ajax') === 'true';
4545
var state = ({
4646
url: url, title: title, id: id, content: content, ajax: ajax
4747
});
4848

4949
document.title = title;
5050
if (history.pushState && !$(this).data("pushstate")) {
51-
var pushurl = url.indexOf("ref=addtabs") == -1 ? (url + (url.indexOf("?") > -1 ? "&" : "?") + "ref=addtabs") : url;
51+
var pushurl = url.indexOf("ref=addtabs") === -1 ? (url + (url.indexOf("?") > -1 ? "&" : "?") + "ref=addtabs") : url;
5252
try {
5353
window.history.pushState(state, title, pushurl);
5454
} catch (e) {
@@ -66,27 +66,27 @@
6666
}
6767
});
6868

69-
navobj.on('click', '.close-tab', function (e) {
70-
id = $(this).prev("a").attr("aria-controls");
69+
navobj.on('click', '.close-tab', function () {
70+
var id = $(this).prev("a").attr("aria-controls");
7171
_close(id);
7272
return false;
7373
});
74-
navobj.on('dblclick', 'li[role=presentation]', function (e) {
74+
navobj.on('dblclick', 'li[role=presentation]', function () {
7575
$(this).find(".close-tab").trigger("click");
7676
});
77-
navobj.on('click', 'li[role=presentation]', function (e) {
77+
navobj.on('click', 'li[role=presentation]', function () {
7878
$("a[addtabs=" + $("a", this).attr("node-id") + "]").trigger("click");
7979
});
8080

8181
$(window).resize(function () {
82-
if (typeof options.nav == 'object') {
82+
if (typeof options.nav === 'object') {
8383
var siblingsWidth = 0;
8484
navobj.siblings().each(function () {
8585
siblingsWidth += $(this).outerWidth();
8686
});
8787
navobj.width(navobj.parent().width() - siblingsWidth);
8888
} else {
89-
$("#nav").width($("#header > .navbar").width() - $(".sidebar-toggle").outerWidth() - $(".navbar-custom-menu").outerWidth() - 20);
89+
$("#nav").width($("#header").find("> .navbar").width() - $(".sidebar-toggle").outerWidth() - $(".navbar-custom-menu").outerWidth() - 20);
9090
}
9191
_drop();
9292
});
@@ -98,36 +98,41 @@
9898
conid = 'con_' + opts.id;
9999
url = opts.url;
100100
url += (opts.url.indexOf("?") > -1 ? "&addtabs=1" : "?addtabs=1");
101+
102+
var tabitem = $('#' + tabid, navobj);
103+
var conitem = $('#' + conid, tabobj);
104+
101105
navobj.find("[role='presentation']").removeClass('active');
102106
tabobj.find("[role='tabpanel']").removeClass('active');
107+
103108
//如果TAB不存在,创建一个新的TAB
104-
if ($("#" + tabid).size() == 0) {
109+
if (tabitem.size() === 0) {
105110
//创建新TAB的title
106-
title = $('<li role="presentation" id="' + tabid + '"><a href="#' + conid + '" node-id="' + opts.id + '" aria-controls="' + id + '" role="tab" data-toggle="tab">' + opts.title + '</a></li>');
111+
tabitem = $('<li role="presentation" id="' + tabid + '"><a href="#' + conid + '" node-id="' + opts.id + '" aria-controls="' + id + '" role="tab" data-toggle="tab">' + opts.title + '</a></li>');
107112
//是否允许关闭
108113
if (options.close && $("li", navobj).size() > 0) {
109-
title.append(' <i class="close-tab fa fa-remove"></i>');
114+
tabitem.append(' <i class="close-tab fa fa-remove"></i>');
110115
}
111116
//创建新TAB的内容
112-
content = $('<div role="tabpanel" class="tab-pane" id="' + conid + '"></div>');
117+
conitem = $('<div role="tabpanel" class="tab-pane" id="' + conid + '"></div>');
113118
//是否指定TAB内容
114119
if (opts.content) {
115-
content.append(opts.content);
120+
conitem.append(opts.content);
116121
} else if (options.iframeUse && !opts.ajax) {//没有内容,使用IFRAME打开链接
117122
var height = options.iframeHeight;
118-
content.append('<iframe src="' + url + '" width="100%" height="' + height + '" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling-x="no" scrolling-y="auto" allowtransparency="yes"></iframe></div>');
123+
conitem.append('<iframe src="' + url + '" width="100%" height="' + height + '" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling-x="no" scrolling-y="auto" allowtransparency="yes"></iframe></div>');
119124
} else {
120125
$.get(url, function (data) {
121-
content.append(data);
126+
conitem.append(data);
122127
});
123128
}
124129
//加入TABS
125-
if ($('.tabdrop li').size() > 0) {
126-
$('.tabdrop ul').append(title);
130+
if ($('.tabdrop li', navobj).size() > 0) {
131+
$('.tabdrop ul', navobj).append(tabitem);
127132
} else {
128-
navobj.append(title);
133+
navobj.append(tabitem);
129134
}
130-
tabobj.append(content);
135+
tabobj.append(conitem);
131136
} else {
132137
//强制刷新iframe
133138
if (options.iframeForceRefresh) {
@@ -138,27 +143,31 @@
138143
}
139144
localStorage.setItem("addtabs", $(this).prop('outerHTML'));
140145
//激活TAB
141-
$("#" + tabid).addClass('active');
142-
$("#" + conid).addClass("active");
146+
tabitem.addClass('active');
147+
conitem.addClass("active");
143148
_drop();
144149
};
145150

146151
var _close = function (id) {
147152
var tabid = 'tab_' + id;
148153
var conid = 'con_' + id;
154+
var tabitem = $('#' + tabid, navobj);
155+
var conitem = $('#' + conid, tabobj);
149156
//如果关闭的是当前激活的TAB,激活他的前一个TAB
150-
if (obj.find("li.active").not('.tabdrop').attr('id') == tabid) {
151-
if ($("#" + tabid).prev().not(".tabdrop").size() > 0) {
152-
$("#" + tabid).prev().not(".tabdrop").find("a").trigger("click");
153-
} else if ($("#" + tabid).next().size() > 0) {
154-
$("#" + tabid).next().trigger("click");
157+
if (obj.find("li.active").not('.tabdrop').attr('id') === tabid) {
158+
var prev = tabitem.prev().not(".tabdrop");
159+
var next = tabitem.next().not(".tabdrop");
160+
if (prev.size() > 0) {
161+
prev.find('a').trigger("click");
162+
} else if (next.size() > 0) {
163+
next.find('a').trigger("click");
155164
} else {
156-
$(">li:last > a", navobj).trigger('click');
165+
$(">li:not(.tabdrop):last > a", navobj).trigger('click');
157166
}
158167
}
159168
//关闭TAB
160-
$("#" + tabid).remove();
161-
$("#" + conid).remove();
169+
tabitem.remove();
170+
conitem.remove();
162171
_drop();
163172
options.callback();
164173
};
@@ -171,11 +180,11 @@
171180
$.fn.refreshAddtabs = function () {
172181
var navobj = $(this);
173182
var dropdown = $(".tabdrop", navobj);
174-
if (dropdown.size() == 0) {
175-
var dropdown = $('<li class="dropdown pull-right hide tabdrop"><a class="dropdown-toggle" data-toggle="dropdown" href="javascript:;">' +
183+
if (dropdown.size() === 0) {
184+
dropdown = $('<li class="dropdown pull-right hide tabdrop"><a class="dropdown-toggle" data-toggle="dropdown" href="javascript:;">' +
176185
'<i class="glyphicon glyphicon-align-justify"></i>' +
177186
' <b class="caret"></b></a><ul class="dropdown-menu"></ul></li>');
178-
dropdown.appendTo(navobj);
187+
dropdown.prependTo(navobj);
179188
}
180189

181190
//检测是否有下拉样式
@@ -190,11 +199,11 @@
190199
//检查超过一行的标签页
191200
var litabs = navobj.append(dropdown.find('li')).find('>li').not('.tabdrop');
192201
var totalwidth = 0;
193-
litabs.each(function (i, j) {
202+
litabs.each(function () {
194203
totalwidth += $(this).outerWidth(true);
195204
});
196205
if (navobj.width() < totalwidth) {
197-
litabs.each(function (i, j) {
206+
litabs.each(function () {
198207
liwidth += $(this).outerWidth(true);
199208
if (liwidth > maxwidth) {
200209
dropdown.find('ul').append($(this));
@@ -203,7 +212,7 @@
203212
});
204213
if (collection > 0) {
205214
dropdown.removeClass('hide');
206-
if (dropdown.find('.active').length == 1) {
215+
if (dropdown.find('.active').length === 1) {
207216
dropdown.addClass('active');
208217
} else {
209218
dropdown.removeClass('active');

0 commit comments

Comments
 (0)